Carry $_POST value one page to another?

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
kibosh
Forum Newbie
Posts: 2
Joined: Sat Mar 15, 2008 6:58 pm

Carry $_POST value one page to another?

Post 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!
bertfour
Forum Commoner
Posts: 45
Joined: Fri Mar 07, 2008 7:33 am

Re: Carry $_POST value one page to another?

Post by bertfour »

Where is the "action" of your form?
kibosh
Forum Newbie
Posts: 2
Joined: Sat Mar 15, 2008 6:58 pm

Re: Carry $_POST value one page to another?

Post 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?
bertfour
Forum Commoner
Posts: 45
Joined: Fri Mar 07, 2008 7:33 am

Re: Carry $_POST value one page to another?

Post 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" !
bertfour
Forum Commoner
Posts: 45
Joined: Fri Mar 07, 2008 7:33 am

Re: Carry $_POST value one page to another?

Post 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>
 
 
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Carry $_POST value one page to another?

Post by Christopher »

Use hidden inputs to carry values forward through succeeding forms.
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Carry $_POST value one page to another?

Post by RobertGonzalez »

And to simplify things, you could put all of this code in one page instead of forwarding page to page to page.
Post Reply