Jump to content

Trouble passing variables through url of a second page


drayarms

Recommended Posts

Here is a simplified version of a script which I  named register.php that processes user input from a registration form.



<?php
if (isset($_POST['submitted'])) {

$firstname = mysql_real_escape_string($_POST['firstname']);

$lastname = mysql_real_escape_string($_POST['lastname']);

header("Location: register_confirm_page.php?firstname=$firstname&lastname=$lastname");

}
?>

 

As intended, the firstname and lastname posted variables are carried over to a redirected page register_confirm_page.php  where the user is supposed to review his registration details before clicking the final submit button to be registered.  Below is a simplified version of the register confirm page:

So far everything works just fine as intended.


<?php

//Retrieve variables from previous page

$firstname=$_GET['firstname'];

$lastname=$_GET['lastname'];

//Show user his details

echo "Firstname:" .$firstname. "<br/>"  ; 
echo "Lastname:" .$lastname

//Show the submit button.
echo'<form method="post" action="register_confirm.php?firstname=$firstname&lastname=$lastname"> <input type="submit" name="submit" value="Register!"  /> </form>   ';

?>

 

Now the intent is to send the first name and last name to yet another page register_confirm.php, where the value is finally submitted into my database.  To keep it simple, I won't include this third page, but what I basically did there was use the GET method as above, to retrieve the values again.  Well this time around, the first and last names are not sent to the third page.  Instead, the literal values '$firstname' and '$lastname' are passed through the url to the third page and I know this because that's what shows up in the url when I click the submit button and that's also what shows up in my database table.  Does anyone understand why this may be happening and not what I expected would happen?  I have passed variables between more than 2 pages before but this is the first time I am encountering this problem.  I really can't see what is being done wrong here. Perhaps someone can.

Link to comment
Share on other sites

As suggested by the others, the single quotes in the last echo statement make it so $firstname and $lastname won't work:

 

echo'<form method="post" action="register_confirm.php?firstname=$firstname&lastname=$lastname"> 

 

 

You could change it to a double quote or do something like:

 

echo'<form method="post" action="register_confirm.php?firstname=' . $firstname . '&lastname=' . $lastname . '"> 

Link to comment
Share on other sites

Truthfully, when I first came to this forum yes, now no

But... You just did that even right now, with this:

There's a hell of an echo in here.

Yeah. No need to repeat something that's already been said. OP, if you need further assistance with this let us know and we will be glad to help

 

You just repeated someone else alluding to not repeating things...

Link to comment
Share on other sites

Truthfully, when I first came to this forum yes, now no

But... You just did that even right now, with this:

There's a hell of an echo in here.

Yeah. No need to repeat something that's already been said. OP, if you need further assistance with this let us know and we will be glad to help

Can't argue with that.

 

You just repeated someone else alluding to not repeating things...

Can't argue with that. I did the very thing I said you shouldn't do. My bad

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.