bobthebullet990 Posted February 12, 2009 Share Posted February 12, 2009 Hi guys, I've tried pretty much everything I can think of without using cookies, even tried sessions, but they don't seem to work either! here's an example script I wrote to show my problem... <?php $input = 1; $myval = "none"; if (isset($_POST['test1'])) { $input=2; $myval="first"; $extra="extra";} if (isset($_POST['test2'])) { $input=3; $myval="second"; } ?> <html> <form name="1" action="" method="post"> <p>just press the button</p> <input type="text" name="in1" value="<?php echo $myval; ?>"> <?php if ($input==1) { ?><input type="submit" name="test1" value="test1"><?php } ?> <?php if ($input==2) { ?><input type="submit" name="test2" value="test2"><?php } ?> <?php if ($input==3) { ?><p>finished, myval is <?php echo $extra; } ?></p> </form> </html> OK, So my problem is that I have a fairly large script with various forms, all I want to be able to is to get php to remember the values stored in variables and not reset all variables when the script is re-loaded, if you run the above script, the variable "$extra" is forgotten by the time I want to use it, how can I get php to retain this value without using a database or cookies? Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/144925-how-to-remeber-variables-in-php/ Share on other sites More sharing options...
gevans Posted February 12, 2009 Share Posted February 12, 2009 You could send it with the form in a hidden field; <input type="hidden" value="<?php echo $extra ?>" name="extra" /> Quote Link to comment https://forums.phpfreaks.com/topic/144925-how-to-remeber-variables-in-php/#findComment-760483 Share on other sites More sharing options...
bobthebullet990 Posted February 12, 2009 Author Share Posted February 12, 2009 only problem doing that is there are 24 input boxes on the page! ...Its a script to configure test devices... 1. user enters config data for 8 devices 2. user chooses a filename and hits submit 3. script validates input, if no errors, then give file to user to download 4. script finds invalid input and asks the user to either download incomplete config file (maybe there are some broken devices that don't need a config) or to go back and change some values if they made a mistake. The final bit is where I'm at, I've collected all the data and processed it, but now putting in step 3.5 where the script finds invalid data, then shows another button to continue anyway, as soon as that button is pressed I loose all data from the first button press! So yeah, could do it your way, but there must be a simpler way surely? Quote Link to comment https://forums.phpfreaks.com/topic/144925-how-to-remeber-variables-in-php/#findComment-760502 Share on other sites More sharing options...
Prismatic Posted February 12, 2009 Share Posted February 12, 2009 For the value of each element you wish to repopulate assign a php variable to it <input type="text" name="something" value="$something_value"> Then in your script you would simply do $something_value = $_POST['something']; //Other code... //Code to display the form(s) Quote Link to comment https://forums.phpfreaks.com/topic/144925-how-to-remeber-variables-in-php/#findComment-760507 Share on other sites More sharing options...
KevinM1 Posted February 12, 2009 Share Posted February 12, 2009 You essentially want a sticky form. Something like: <?php if(isset($_POST['submit'])) { if(isset($_POST['in1'])) { $in1 = $_POST['in1']; } /* validate and assign the other inputs */ if(/* inputs all are valid */) { /* redirect the user to the file download page */ } else { /* errors - generate error output, and store in a variable */ } } else //form not submitted, or submitted with bad values { if(/* error output variable exists */) { /* output error */ } ?> <!-- HTML FORM --> <form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="in1" value="<?php if(isset($in1)) { echo $in1; } ?> /> <!-- repeat with other inputs --> </form> <?php } //end else ?> Quote Link to comment https://forums.phpfreaks.com/topic/144925-how-to-remeber-variables-in-php/#findComment-760512 Share on other sites More sharing options...
dave_2008 Posted February 12, 2009 Share Posted February 12, 2009 Why are you not using sessions? If using sessions, start each script with session_start(<-- click) <?php session_start(); /* * */ $input = 1; $myval = "none"; if (isset($_POST['test1'])) { $input=2; $myval="first"; $extra="extra";} if (isset($_POST['test2'])) { $input=3; $myval="second"; } ?> <html> <form name="1" action="<? /* * */ echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>just press the button</p> <input type="text" name="in1" value="<?php echo $myval; ?>"> <?php if ($input==1) { ?><input type="submit" name="test1" value="test1"><?php } ?> <?php if ($input==2) { ?><input type="submit" name="test2" value="test2"><?php } ?> <?php if ($input==3) { ?><p>finished, myval is <?php echo $extra; } ?></p> </form> </html> Any maybe some more changes here and there. If you want to ensure that people who do not have cookies enabled should be able to access the page, then you can pass the PHP session id(<-- click) as a hidden value or as part of the link, but that is not wise from a security point of view - just good to understand the concept and get the thing working... Quote Link to comment https://forums.phpfreaks.com/topic/144925-how-to-remeber-variables-in-php/#findComment-760542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.