Page 1 of 1

Variable values lost on form submit

Posted: Thu Jun 11, 2009 4:27 pm
by prologman
Hi,
I'm new at PHP. I've done a search for this problem on this forum but can't find the exact solution.

I have a file "fred.php" which contains a form, when submitted calls itself - "fred.php". Various fields are (or will be!) created/removed/updated in the form as the user fills in fields and presses the "submit" button.

But I cannot get the page to remember a value entered in an input field after the user presses submit.

I have tried using $_SESSION, but without success. Maybe this is supposed to be used to recall variables in a different session (so I'm not sure why it doesn't work anyway), but my problem is in the same session, just a [recursive] refresh of the page. So maybe I need to use something else.

Code: Select all

   if (!isset($_SESSION['joe'])) {
        $_SESSION['joe']=1;
    }else{
        $joe++;
    }
 
- $_SESSION['joe'] is always 'not set' when the page is refreshed/recalled.

Any guidance would be greatly appreciated.

cheers
Steve

Re: Variable values lost on form submit

Posted: Thu Jun 11, 2009 4:50 pm
by Christopher
Two basic things:

1. The values from a form are in $_POST (or $_GET)

2. $_SESSION is not loaded with values unless session_start() is called.

Re: Variable values lost on form submit

Posted: Thu Jun 11, 2009 5:51 pm
by mikemike
I normally do something like:

Code: Select all

<form action="" method="post">
  Username: <input type="text" name="name" value="<?php echo (empty($_POST['name']) ? '' : $_POST['name']); ?>" />
  Email: <input type="text" name="email" value="<?php echo (empty($_POST['email']) ? '' : $_POST['email']); ?>" />
...