Page 1 of 1

How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 1:26 pm
by kev2011
Hey guys,

Hopefully someone could help me out with some basic php. I've been trying to figure it out all night but I'm out of ideas lol.

The way my site is set up is there's a landing page (page 1) that goes to the registration page (page 2), then on the registration page there's a form that the user enters their info like first name, last name, email etc and then when that form is completed I've been trying to get the form to pass the variables to the next page. The form's method is 'post'. Here's the form's code -

Code: Select all

<form id="signupForm" name="signupForm" method="post" action="registration_submit.php">
							<p>
								<label for="firstname">First Name:</label>
								<input id="firstname" name="firstname" class="required inputWidth" type="text">
							</p>
							<p>
								<label for="lastname">Last Name:</label>
								<input id="lastname" name="lastname" class="required inputWidth" type="text">
							</p>
							<p>
								<label for="email">Email:</label>
								<input name="email" id="email" class="required email inputWidth" type="text">
							</p>
							<p>
							<input name="agree" value="no" type="hidden">
							<input name="agree" value="yes" checked="checked" type="checkbox"> <span style="font-size: 10px;">Yes, I would like to receive new offers and information from TheConsumerWinner.com.</span> </p>
							<p>
								<input name="submit" id="submit" style="margin: 0pt 0pt 0pt 150px; padding: 7px; font: bold 12px Arial;" value="Register now!" type="submit">
							</p>
                            
</form>
However the next page is a redirect link (page 3) & I'm trying to figure out how to pass the variables from the form into the redirect link, I've got the correct code for the variables and I don't think that's the issue, but I get the feeling I need to use POST or GET php methods. I can post the form data on to the page 3 by using -

Code: Select all

<?php

$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];

echo "Here is the first name: ". $firstname. ".<br />";
echo "Here is the email: ". $email.".<br />";

?>
and it shows the first name & email from page 2, but what I need to do is pass the variables in to the redirect code so I tried this -

Code: Select all

<?php

$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];

echo header( 'Location: http://mysite.com/test&fname=$firstname' ) ;

?>
But the data does not get passed by using $firstname and I've tried a bunch of different php but can't figure it out

Really appreciate anyone's help in advance!

Kev

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 1:31 pm
by AbraCadaver
It will work if you quote it properly: http://us2.php.net/manual/en/language.types.string.php

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 1:50 pm
by kev2011
Thanks for the tip.

What do I need to quote? My html knowledge is high but I'm new to php so not sure what I should be quoting

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:11 pm
by tnrh1
Try
$_POST Instead of $_REQUREST

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:16 pm
by kev2011
I tried it like this -

<?php

$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$email = $_REQUEST['email'];

echo header( 'Location: http://mysite.com/test&fname=$firstname' ) ;

?>


But still no luck in passing the variable to the redirect URL

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:25 pm
by tnrh1
Take this:

Code: Select all

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
and this is the welcome.php :

Code: Select all

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:38 pm
by AbraCadaver
kev2011 wrote:Thanks for the tip.

What do I need to quote? My html knowledge is high but I'm new to php so not sure what I should be quoting
Read the link I posted. It will save you hours of frustration later. Variables aren't parsed inside of single quotes, so do either of these:

Code: Select all

header('Location: http://mysite.com/test&fname=' . $firstname);
exit;
//or
header("Location: http://mysite.com/test&fname=$firstname");
exit;
And you should use exit; after a header location.

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:40 pm
by kev2011
JohnO1990 wrote:Your form sends the variables to the action=""
so on that page just use $_POST[''];
to get your variables. :)
Hmm. So how do I use POST to place the data from the form page in to the redirect URL as a variable on the next page?

I tried -

Code: Select all

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];

echo header( 'Location: http://mysite.com/test&firstname=$firstname&lastname=$lastname&email=$email' ) ;

?>
But that code didn't read the variables, it just showed the redirect url as http://mysite.com/test&firstname=$first ... ail=$email & it didn't pass the data to the redirect URL

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:42 pm
by kev2011
tnrh1 wrote:Take this:

Code: Select all

<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
and this is the welcome.php :

Code: Select all

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
This posts the data on to the next page, but I need it to post the data as variables into the redirect URL on the next page

Appreciate you help guys :)

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:44 pm
by AbraCadaver
AbraCadaver wrote:
kev2011 wrote:Thanks for the tip.

What do I need to quote? My html knowledge is high but I'm new to php so not sure what I should be quoting
Read the link I posted. It will save you hours of frustration later. Variables aren't parsed inside of single quotes, so do either of these:

Code: Select all

header('Location: http://mysite.com/test&fname=' . $firstname);
exit;
//or
header("Location: http://mysite.com/test&fname=$firstname");
exit;
And you should use exit; after a header location.
Plus, don't echo the header call.

Re: How To Pass Variable From A Form To Next Page

Posted: Wed Mar 09, 2011 2:50 pm
by kev2011
AbraCadaver wrote:
AbraCadaver wrote:
kev2011 wrote:Thanks for the tip.

What do I need to quote? My html knowledge is high but I'm new to php so not sure what I should be quoting
Read the link I posted. It will save you hours of frustration later. Variables aren't parsed inside of single quotes, so do either of these:

Code: Select all

header('Location: http://mysite.com/test&fname=' . $firstname);
exit;
//or
header("Location: http://mysite.com/test&fname=$firstname");
exit;
And you should use exit; after a header location.
Plus, don't echo the header call.

You the man! Thanks AbraCadaver, it works. :) You just saved me hours of banging my head against the screen.

I'll still read through the link you sent to make sure I understand it.

If we ever cross paths drinks are on me all night!