Loosing values in input fields when browsing from page to page

Hi, I have converted my simple data imput forms for few members to sessions mode. Several hours of work but now ready and quite happy with the results except ONE! Before session control, if a user fills a form, procede to next step to fill more and the browse back to edit something, the input fields held the previous data.

With sessions, that data is lost and all fields become cleared. This is a huge disadvantage and members are complaining. How can I prevent this from happening in session mode? I hope for a simple non-cookie solution, perhaps a setting.

Thanks and hope tofind help on this because it is annoying to rewrite data input from scratch.

My guess is that the initial page that sets the SESSIONs is not checking to see if they already exist.
Might this be the case?

The input form is initially blank, so there is nothing to be checked or retrieved. The user starts filling the form and moves to the next page (submit form) for more editing. Values are passed and retrieved by the usuall GET/POST.

Clicking the back button of the browser would have left the data inputted previously in the fields, making typo corrections or editting very easy, but now, with the sessions, the fields are cleared and the user have to enter them from scratch.

That’s because you are not loading the values from the session if the session does exist.

… and you have session_start(); at the top of both pages before any output is sent to browser I assume.
Then you can set variables to be echoed in your form like so.

$firstname = (isset($_SESSION['firstname']) ? $_SESSION['firstname'] : '');

Hi guys, yes I figured out the reason behind and had to o into some coding to get it sorted. Yes, session_start() in all page starts.

I took the long cut where in the data receiving page I set all variable as:
$_SESSION[‘king’] = $king = trim($_POST[“kingdom”]);

then in the form page (data sending page) i put in each field value =“$_SESSION[‘varname’]” so in my example above value=“$_SESSION[‘king’]”

Drummin, you code is too hard for my php IQ. Could you explain the logic behind?

$firstname (the usable variable) becomes the session stored variable (if it is set) but then I did not follow behind the ?

After that I was glad that I had resolved all problem when migrating my forms to sessions, now I have an newly emerged problem. When I go back to the form to input a new entry, all data is retained. When I click the clear button (<input type=“reset”) the field dont’ clear??? what should I do?

No, that’s the short-hand version of checking if the session was set. Instead of having something like

if(isset($_SESSION['firstname'])) {
    $firstname = $_SESSION['firstname'];
}

You have something like what Drummin had posted.

It basically means; if $_SESSION[‘firstname’] is set, assign the $firstname variable to it. If it’s empty (notice the single quotes near the end of the equation; single and double quotes with nothing in them means that it is empty), don’t assign the $firstname variable to it.

The ? basically is equivalent to else in the if-else statement.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.