Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/271839-session-variables/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/271839-session-variables/#findComment-1398634
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/271839-session-variables/#findComment-1398732
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.