Forum Moderators: coopster
I have a php page that at the beginning, initializes an array and variables, displays colored buttons, and then when a button is pressed, it is supposed to set a variable to a number based on the button pressed. The page is then redrawn but the variable is reset to nothing. (The variable from the button is supposed to change the input text background to the selected color).
Correct me if I'm wrong, but I guess every time you press a button, the page is re-drawn and the variables start from scratch.
What is the best way to "remember" what button was pressed when the page is re-drawn? Do I need to write it out to SQL or a file? Thanks.
To maintain "state", we use techniques such as passing variables in the query string, hidden form fields, cookies, and sessions.
If you are using a form that is posting to itself, one of the quickest/easiest ways to see how things are working is to print out the $_POST superglobal array and you'll see your form data. You can use this form data on your page again by extracting it from the $_POST superglobal array.
For example, at the top of your script, check if the form has already been posted an if it has, use the posted variable to set the initial value of your existing variable.
<?php
$myVariable = (isset [php.net]($_POST['myVariable'])) ? $_POST['myVariable'] : 'defaultValue';
?>
<input type="text" value="<?php print htmlentities($myVariable ); ">
Welcome to WebmasterWorld, wtbard ;)