Validating a form then sending $_POST data to second page

Hi,

My forms validation is on the same page as the form itself, so when the user hits ‘Submit’ it reloads the page and checks through the validation code. If everything is OK it then sends the user to a second page, where the data is added to the MySql table.

However, I am having some trouble with getting the $_POST data to the second page after the first page has already used it.

Is there something different I need to do to send the data to the second page if I am already using it on the first page?

Or is there a better way of achieving what I am trying to do?

Here is my PHP code:

[php]<?php

if (isset($_POST[‘username’], $_POST[‘realname’], $_POST[‘password’], $_POST[‘repassword’], $_POST[‘email’]))
{
$errors = array();

$username = $_POST['username'];
$realname = $_POST['realname'];
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$email = $_POST['email'];

if (empty($username) || empty($realname) || empty($password) || empty($repassword)|| empty($email)) 
  {
    $errors[] = 'All fields are required';
  }
else
  {
    if(strlen($username) > 25 ) $errors[] = '&#8226; Username can not be longer than 12 characters';
    if(strlen($username) < 3 ) $errors[] = '&#8226; Username must be 3 characters or more';

if(preg_match(’/[^0-9A-Za-z]/’,$username)) $errors[] = ‘•asdfadfafadfadfadsf’;
if(strlen($realname) < 2 ) $errors[] = ‘• Real name can not be shorter than 2 characters’;
if(strlen($password) < 5 ) $errors[] = ‘• Password must be 5 characters or more’;
if($password !== $repassword) $errors[] = ‘• Passwords do not match’;
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) $errors[] = ‘• Please enter a valid email address’;
}

if(!empty($errors))
  {
    foreach($errors as $error)

{

}
}
else
{

header(“Location: adduser.php”);
}
}
?>

[/php]

Thank you

I have just thought I could put all the data into the session and then load it on the second page from the session. Is that the best way to do it?

Hi Penny94,

While you could specifically populate your session variable with the results and then redirect as you are trying to, a much better way to handle this is to move all of your validation to the adduser.php page. You would basically be doing the same thing you are here, but instead of the redirect if everything is valid, you simply continue to process the data.

As you have discovered, $_POST data will not follow a redirect. If you want the validation error to appear on the form page, you could have your validation and table write page redirect on validation failure back to the form’s page with a server query string attached to the redirect url. You would then just need to add a check for this return and display it on your form.

For example, in your adduser.php page you would start by validating the $_POST data. If it is valid you add it to the table. If not, you redirect to: [php]header(“Location: yourform.php?error=invalid”);
[/php]
You would then add something along the lines of:[php]if(isset($_GET[‘error’]) && $_GET[‘error’] == ‘invalid’) echo ‘Your submission was not valid.’;[/php]

If this doesn’t fit what you need, let me know and I’ll try to help you find a different solution.

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service