Forum Moderators: coopster

Message Too Old, No Replies

Passing Form Data Problem

New to PHP

         

mctavish

10:25 am on Feb 25, 2004 (gmt 0)

10+ Year Member



I need to pass form data from page to page and to test how to do it with PHP I've used the following code. Page 2 displays the name and age OK, but page 3 "loses" them and just displays the age. Where am I going wrong?

Code for Page1.php

<form action="page2.php" method="post">
<input name="name" type="text">
<input name="age" type="text">
<input name="" type="submit">
</form>

Code for Page2.php

<?php echo $_POST["name"]?>
<?php echo $_POST["age"]?>
<form action="page3.php" method="post">
<input name="name" type="hidden" value="<? $_POST['name']?>">
<input name="age" type="hidden" value="<? $_POST['age']?>">
<input type="text" name="sex">
<input name="" type="submit">
</form>

Code for Page3.php

<?php echo $_POST['name']?>
<?php echo $_POST['age']?>
<?php echo $_POST['sex']?>

digitsix

11:15 am on Feb 25, 2004 (gmt 0)

10+ Year Member



For page2, try this:

<?php echo $_POST["name"];?>
<?php echo $_POST["age"];?>
<form action="page3.php" method="post">
<input name="name" type="hidden" value="<? echo $_POST['name'];?>">
<input name="age" type="hidden" value="<? echo $_POST['age'];?>">
<input type="text" name="sex">
<input name="" type="submit">
</form>

Try to make a habit of using semi colons (;) after each line of code unless you are starting a while loop, for loop or if statement, u know...

mctavish

11:38 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Thanx a lot, why the "echo" when assigning the value though?

coopster

1:06 pm on Feb 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, mctavish!

You are writing out the value at this point, not assigning it.

You have to use echo because you are "writing" [php.net] the HTML on-the-fly. In order to write out the value of the variable, you need to use PHP's echo [php.net] or print [php.net] language constructs.

mctavish

2:05 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



Got it! Thanks a lot.