mbrittb00 Posted December 8, 2009 Share Posted December 8, 2009 I have seen this done on various websites so I know it can be done. I have a php file that contains a form with several data entry fields and a section where the results of calculations based on that input data are displayed. What I would like to have happen is when the user clicks the "submit" button on the form php file is redisplayed but with the entry data that was supplied still there (not the default value). I have actually been able to accomplish this by making a !isset($_COOKIES[]) and setcookies calls for each of the form fields, then have the form 'submit' execute a different php file that sets the COOKIES from the form POST data, then redirects back to the origional php file. The problem with this is that I have will have to have an !isset block for each of the form fields then in the alternate file, I will have to have another set of setcookies calles. This very tedious, and I'm sure there is a better way. The websites I have found that do this, when I look at the source code (initially before any form entries are made) lists the value of the form fields as "" (i.e. value=""). Then after entering data and clicking submit, that same source code lists the values as the the data that was entered (i.e. value="100"). The form is set to execute a a different php file called cookies.php (action="cookies.php"). Unfortunately I am not able to see the source code for the cookies.php file, so I don't know what is going on there. Any assistance would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/ Share on other sites More sharing options...
iPixel Posted December 8, 2009 Share Posted December 8, 2009 Can we get an example of these websites to get a much clearer picture of how you need this to work? Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973504 Share on other sites More sharing options...
smerny Posted December 8, 2009 Share Posted December 8, 2009 i think what you mean is something like this? <form method="POST" action="<?php echo $_SERVER['php_self'] ?>"> <input type="text" name="name" value="<?php echo $_POST['name'] ?> " /> <input type="submit" value="submit" name="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973506 Share on other sites More sharing options...
mbrittb00 Posted December 8, 2009 Author Share Posted December 8, 2009 Here is a site that is an example. You can look at the source code before an after submitting data. http://www.glacialis.uuuq.com/calculator.php Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973518 Share on other sites More sharing options...
smerny Posted December 8, 2009 Share Posted December 8, 2009 that's done the same way as what i posted Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973520 Share on other sites More sharing options...
mbrittb00 Posted December 8, 2009 Author Share Posted December 8, 2009 Ok thanks. I just realized that when you "View Page Source" all the php code is 'executed' and replaced by the result. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973526 Share on other sites More sharing options...
smerny Posted December 8, 2009 Share Posted December 8, 2009 yep, the browser only receives the information you send it (ie: "echo", "print", or stuff outside of php tags) Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973530 Share on other sites More sharing options...
aeroswat Posted December 8, 2009 Share Posted December 8, 2009 That's not a one size fits all. It doesn't work for anything but textboxes and textareas I believe Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973532 Share on other sites More sharing options...
mbrittb00 Posted December 8, 2009 Author Share Posted December 8, 2009 It seems to be working but the <?php echo $_SERVER['php_self'] ?> call is returning an empty string (at least as far as HTML is concerned. Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973540 Share on other sites More sharing options...
aeroswat Posted December 8, 2009 Share Posted December 8, 2009 It seems to be working but the <?php echo $_SERVER['php_self'] ?> call is returning an empty string (at least as far as HTML is concerned. what version of php are u running? Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973542 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 PHP is CaSe SenSitIvE: <?php echo $_SERVER['PHP_SELF']; ?> Would be the proper way to call that variable. Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973545 Share on other sites More sharing options...
Philip Posted December 8, 2009 Share Posted December 8, 2009 echo $_SERVER['PHP_SELF']; // note uppercase That should almost always work (unless you're running like before 4.3 - even so you really need to update your software ) Also, a blank action in a form defaults to the page it is on. Quote Link to comment https://forums.phpfreaks.com/topic/184420-how-to-save-form-data-and-then-re-populate-on-submit/#findComment-973551 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.