Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Javascript overriding PHP generated form field on page reload

  • 23-02-2011 12:50PM
    #1
    Registered Users, Registered Users 2 Posts: 1,657 ✭✭✭


    I have a web page, which populates a hidden form field using PHP.
    Say
    <input type="hidden" name="field" id="field" value="newval1" />
    (newval1 was the result of <?php echo $field; ?>)



    Now it being interactive, the user can do stuff which will result in the value of the field being over-ridden, using Javascript - document.getElementById("field").value = 'blah'

    Now when the user refreshes the page, the PHP recalculates the value and it inserts it into the form
    <input type="hidden" name="field" id="field" value="newval2" />

    but, the value of the field is still "blah".

    Can I take it this is normal operating procedure, and if so, what is the "correct" way of preventing it? i.e. have the PHP calculated field be the value used until the user changes it.

    I have a bit of Javascript saying (on the page load)
    document.getElementById("field").value = "<?php echo $field; ?>";
    but I'm thinking there must be a right way of doing it...


Comments

  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    komodosp wrote: »
    I have a web page, which populates a hidden form field using PHP.
    Say
    <input type="hidden" name="field" id="field" value="newval1" />
    (newval1 was the result of <?php echo $field; ?>)



    Now it being interactive, the user can do stuff which will result in the value of the field being over-ridden, using Javascript - document.getElementById("field").value = 'blah'

    Now when the user refreshes the page, the PHP recalculates the value and it inserts it into the form
    <input type="hidden" name="field" id="field" value="newval2" />

    but, the value of the field is still "blah".

    Can I take it this is normal operating procedure, and if so, what is the "correct" way of preventing it? i.e. have the PHP calculated field be the value used until the user changes it.

    I have a bit of Javascript saying (on the page load)
    document.getElementById("field").value = "<?php echo $field; ?>";
    but I'm thinking there must be a right way of doing it...

    The logical way (I would think) is to reset the form - using the form reset function, not by setting individual values - via JavaScript.


Advertisement