garyed Posted December 10, 2012 Share Posted December 10, 2012 I've been trying to understand how to keep data entered into a form when the user leaves the page & returns using session variables. I've got it working to some degree but I don't think its right & I'm hoping someone can steer me in the right direction. I've got about 75 field inputs on one page so I'm trying to get it right with just one field before I change everything. Here's my code so far: <html> <head> <?php session_start(); if ($_POST['test']!=""){ $_SESSION['test']=$_POST['test'];} ?> </head> <body> <form method="post" action=""> <input type="text" name="test" value="<?php if (isset($_SESSION['test'])){echo $_SESSION['test'];} else {echo $_POST['test'];} ?>" > <input type="submit" value="submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/271839-session-variables/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2012 Share Posted December 10, 2012 I've got about 75 field inputs You should define the fields (legend text, field name, field type) in an array (or a database table), then simply loop over the list of field definitions and build the form using php code. Quote Link to comment https://forums.phpfreaks.com/topic/271839-session-variables/#findComment-1398622 Share on other sites More sharing options...
garyed Posted December 11, 2012 Author Share Posted December 11, 2012 You should define the fields (legend text, field name, field type) in an array (or a database table), then simply loop over the list of field definitions and build the form using php code. I like that idea but it's a little over my head right now. I know how to do a while loop & populate a drop down menu from a database but I don't know how I could get the order exactly right for each input field & that would be a must. The site I'm working on is: http://www.loadcalc.net . Its a series of four pages where each choice goes to the next page. The last page has the most fields & that is the one where I have the most complaints about losing data when leaving the page & returning. Also the problem I have with this code: session_start(); if ($_POST['test']!=""){ $_SESSION['test']=$_POST['test'];} is once a session variable is set & then you delete the input value in the field, it gets repopulated with the session variable again instead of being blank. I assume there is a better way to do it but haven't figured it out yet. Quote Link to comment https://forums.phpfreaks.com/topic/271839-session-variables/#findComment-1398634 Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2012 Share Posted December 11, 2012 The general fix for your logic would be to put the form processing code into a specific section of code that - tests if the form has been submitted, validates the inputs, then saves the submitted post data to session variables. Empty post data would be saved as an empty string to the corresponding session variable. In the form code for the value='...' attributes, if the corresponding session variable isset, meaning that the form has been submitted one or more times, you would use it's value, which could be an empty string, else use an empty value. Quote Link to comment https://forums.phpfreaks.com/topic/271839-session-variables/#findComment-1398732 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.