Page 1 of 1

Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 7:05 pm
by kibosh
Hi everyone,

I wrote a simple form, and I want to make sure the user has entered values in my fields before forwarding them to my next page. My code is here:

Code: Select all

<?PHP
$name=$_POST['name'];
$age=$_POST['age'];
 
if ($_POST['submit'] == "Submit")
    {
    if ($_POST['name'] == "")
        {
        $name_error="You did not enter your name.";
        }
    else if ($_POST['age'] == "")
        {
        $age_error="You did not enter your age.";
        }
    else
        {
        header("Location: results.php");
        }
    }
 
?>
 
<head>
</head>
 
<body>
<form action="" method="post" name="simpletest">
Name:<br /><?PHP echo $name_error; ?>
<input name="name" type="text" value="<?PHP echo $name; ?>" />
<br />
Age:<br /><?PHP echo $age_error; ?>
<input name="age" type="text" value="<?PHP echo $age; ?>" />
<br />
<input name="submit" type="submit" value="Submit" />
<input name="clear" type="reset" value="Clear" />
</form>
</body>
</html>
If the user has filled in my fields, they should be taken to this page upon submission:

Code: Select all

<?PHP 
$name=$_POST['name'];
$age=$_POST['age'];
?>
 
<head>
</head>
 
<body>
<?PHP
echo "Your name is: ".$name." and you are ".$age." years old.";
?>
</body>
</html>
My variables are not being echoed on the results page. Any idea why not?

Or maybe a better question, is there a better way to verify a user has entered data into a form field before sending the values to another script?

Thanks!

Re: Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 7:20 pm
by bertfour
Where is the "action" of your form?

Re: Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 7:38 pm
by kibosh
I took it out. At first, i had it set to my result.php page. When the action is set though, all my data checks are ignored. Is there a better way to ensure the user has filled out all required fields before sending them to another page; confirmation page for example?

Re: Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 7:46 pm
by bertfour
1st page: the form.

2nd page, read the form variables (and do some checks maybe)

on the same 2nd page you can display information, thank you's, or whatevers after the checks.

And then go anywhere from there....

But, you need the "action" !

Re: Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 7:59 pm
by bertfour
Something like this:

Code: Select all

<html>
<head>
<title>Email Checker</title>
</head>
<body>
<h3>Email Checker</h3>
<form method="post" action="check.php">
<p><input type="text" name="email" size="20">
<input type="submit" value="Submit" name="b">
</form>
</body>
</html>
and then in check.php :

Code: Select all

<html>
<head>
<title>Email Checker Results</title>
</head>
<body>
<h3>Email Checker Results</h3>
<?
 
$email = $_POST['email'];
 
$Pattern = "^([0-9a-z]+)([ 0-9a-z\.-_\-]+)@([0-9a-z\.-_\-]+)\.([0-9a-z]+)";
 
if (eregi($Pattern , $email)) {
 print "This is a valid email address!";
} else {
print "This is NOT a valid email address!";
}
 
<br><br>
Do something here...
?>
</body>
</html>
 
 
 

Re: Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 8:27 pm
by Christopher
Use hidden inputs to carry values forward through succeeding forms.

Re: Carry $_POST value one page to another?

Posted: Sat Mar 15, 2008 9:32 pm
by RobertGonzalez
And to simplify things, you could put all of this code in one page instead of forwarding page to page to page.