Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Javascript overriding PHP generated form field on page reload

  • 23-02-2011 11:50am
    #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