Losing form values after submit.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ACRMIKE
Forum Newbie
Posts: 7
Joined: Sun Jul 05, 2009 4:41 pm

Losing form values after submit.

Post by ACRMIKE »

Hello,
I am new to PHP and borrowed a registration script from a PHP site. I've stripped all the SQL out in the snippet below and left enough to just recreate the issue. I've done a ton of searches to try and figure this out, but to no avail.
The issue that I'm having is that the form variables are being lost after the submit. I would think that $_GET or $_REQUEST would restore those variables so that the user doesn't have to re-enter them if they make a mistake in a field, they submit the form and the form regenerates. I've put the REQUEST and/or GET before and after the form tag, but nothing seems to work.
What on earth am I doing wrong? :banghead:
Thanks,
Mike


<?php
session_start();
include ('dbc.php');
if ($_POST['Submit'] == 'Register')
{
if ( empty($_POST['full_name']) )
{
header("Location: testMe.php?msg=ERROR: Name is empty..");
exit();
}

if (strlen($_POST['email']) < 5)
{
header("Location: testMe.php?msg=ERROR: Incorrect email. Please enter valid email address..");
exit();
}
}
?>
<link href="styles.css" rel="stylesheet" type="text/css">
<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>
<table width="65%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="e5ecf9" class="forumposts">
<form name="form1" method="post" action="testMe.php" style="padding:5px;">
<p><br>
Name:
<?php $full_name=$_GET['full_name']; ?>
<input name="full_name" type="text" id="full_name">
Ex. John Wilson</p>
<p>Email:
<?php $email=$_REQUEST['email']; ?>
<input name="email" type="text" id="email">
Ex. john@domain.com</p>
<p align="center">
<input type="submit" name="Submit" value="Register">
</p>
</form>
</td>
</tr>
</table>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Losing form values after submit.

Post by califdon »

You're not doing anything wrong, you're just misunderstanding how HTTP protocol works. Your assumption that Form values should be somehow be restored after a submit is incorrect. HTTP is a "stateless" protocol. That means that once a request has been sent to a server and the server responds, there is no "connection" maintained between the client and the server. It is up to the program logic of your script to do any restoring or re-creating of forms and values. So a viewer requests a web page that contains an HTML Form. When the Submit button is clicked, it sends a new request to the action= URL and includes the form data as either $_GET or $_POST array variables that the server can read. Nothing else happens except what your script does. Generally, if you want the original viewer to see the form again, you have to re-send the whole web page, only this time you supply the value for each <input> element. There is another technique that's called Ajax that uses Javascript to request data without completely reloading the page, but I'm speaking here of standard PHP and HTML forms.
ACRMIKE
Forum Newbie
Posts: 7
Joined: Sun Jul 05, 2009 4:41 pm

Re: Losing form values after submit.

Post by ACRMIKE »

Thanks for the reply.
Are there any snippets of code out there that do what you are explaining, whether it's javascript or something that would make this work?

<input name="full_name" type="text" id="full_name" value="<?php echo $_REQUEST['full_name']; ?>" />
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Re: Losing form values after submit.

Post by paqman »

ACRMIKE
Forum Newbie
Posts: 7
Joined: Sun Jul 05, 2009 4:41 pm

Re: Losing form values after submit.

Post by ACRMIKE »

Hi,
I was looking for something a little easier than the Online Ajax Tutorial :mrgreen: , but I do seriously appreciate the ideas you have given me.
Anyway, I figured out a way to do this. I know it's probably not the most glamorous solution, but I only have maybe a dozen parameters that I need to "keep alive" and this seems to work under IE and Firefox. I know there is a limit on the length of the parms in the URL, but so far, so good. Can you see any major issues with this approach?
Thanks,
Mike

Code: Select all

<?php 
session_start();
include ('dbc.php'); 
if ($_POST['Submit'] == 'Register')
{
   $params_list = "&full_name=$_POST[full_name]&email=$_POST[email]";
   if ( empty($_POST['full_name']) )
    { 
    header("Location: testMe.php?msg=ERROR: Name is empty..".$params_list);
    exit();
    }
 
   if (strlen($_POST['email']) < 5)
   {
    header("Location: testMe.php?msg=ERROR: Invalid email address..".$params_list);
    exit();
    }
}   
?> 
<link href="styles.css" rel="stylesheet" type="text/css">
<?php if (isset($_GET['msg'])) { echo "<div class=\"msg\"> $_GET[msg] </div>"; } ?>
 
<table width="65%" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td bgcolor="e5ecf9" class="forumposts"> 
      <form name="form1" method="post" action="testMe.php" style="padding:5px;">
        <p><br>
          Name:           
          <input name="full_name" type="text" id="full_name" value="<?php echo $_GET['full_name']; ?>" />
          Ex. John Wilson</p>
        <p>Email: 
          <input name="email" type="text" id="email" value="<?php echo $_GET['email']; ?>" />
          Ex. john@domain.com</p>
        <p align="center"> 
          <input type="submit" name="Submit" value="Register">
        </p>
      </form>
    </td>
  </tr>
</table>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Losing form values after submit.

Post by califdon »

Most of us here in the forum will not bother to read your code posts if you don't enclose them in code tags (at the top of the message box when you are entering a post here) to make it readable, as I have edited your post to do for you.

Well, you're not going to get any data the way you have done it. Your form specifies method="post" (quite properly), so you will not find anything in the $_GET array. If you change those to $_POST, it should work, and that's precisely what I told you that you have to do in my earlier response ("you have to re-send the whole web page, only this time you supply the value for each <input> element"). This is the only way to handle it, other than Ajax. What you need to realize is that you have to reload the page, that is, the server has to send back the entire page to the browser. Again, this is because in HTTP, there is no "connection" established between the server and the browser. It is a "stateless" protocol.

There are some important issues about using either $_GET or $_POST array variables, though, that you need to be aware of. Never use them directly, as you have done. This is because hackers can send code as $_GET or $_POST variables that could bring down your entire system or wipe out every file on your server! Always assign those array variables to regular variables and then validate or "sanitize" them by one of several methods. Don't use the array variables in your logic, use the "sanitized" variables. You can find volumes of information on this by searching for the terms sql injection.
Post Reply