1

How to pass a form value to next page in other form?

I have this code in the :

<form action="confirm.php" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Email: <input type="text" name="email">
</form>

Now I want that in confirm.php to put some hidden input fields with this values, I tried this code:

<form action="nextpage.php" method="post">

//some other input fields...

<input type="hidden" name="firstname" value="<?php $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php $_POST['email']?>">
</form>

And so on in other 2 pages, and in the last page I want to email all this fields, I tried PHP _SESSION, but no luck with that, so I think that this can be more easier for me!

And something else I forgot to tell, on the second page (nextpage.php) action form variable I refer to a file that use this code:

<?php
header('Location: dear-'.$_POST['firstname'].'.php');
?>

<html>
<form>
  <input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
  <input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
  <input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
</html>

In this case how to pass this values (firstname, lastname and email) on the next page ? I use that because I want to generate a page like this www.site.com/dear-name.php

Sarah Collin
  • 27
  • 1
  • 1
  • 5

5 Answers5

2

Try this in confirm.php

<form action="nextpage.php" method="post">

//some other input fields...

<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
Jenz
  • 8,280
  • 7
  • 44
  • 77
1

You forgot to echo your variable.

<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">

Update:

You have two choices :

  1. Store your data to session and retrieve to another page.
  2. Pass your data using query string.

    header('Location: dear-'.$_POST['firstname'].'.php?firstname='.$_POST['firstname'].'&lastname='.$_POST['lastname'].'&email='.$_POST['email']);
    
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
0

use echo:

<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']; ?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']; ?>">

and if this still not working, isset() your post variables:

if(isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])) {
   $firstname = $_POST['firstname'];
   $lastname  = $_POST['lastname'];
   $email     = $_POST['email'];

   echo '<input type="hidden" name="firstname" value="$firstname">';
   echo '<input type="hidden" name="lastname" value="$lastname">';
   echo '<input type="hidden" name="email" value="$email">';
}
Vainglory07
  • 5,073
  • 10
  • 43
  • 77
  • I get this error Parse error: syntax error, unexpected T_IF in /public_html/test/dear.php on line 4. The line 4 is this code: if(isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])) { – Sarah Collin Nov 21 '13 at 05:24
0

In your input text field, value is empty because of echo is missing. You need to add echo to print the value in your form. So that it can be sent into another page.

Krish R
  • 22,583
  • 7
  • 50
  • 59
0

The second option is that you can use session_start() in top of each page. and store the value of the variable in your session.

$_SESSION['name'] = "value_of_session"// in your case that is POST data

and this can be used in next page you need not to include hidden fields.

the most important thing is that You must write

<?php session_start()  ?> // in each(you've three) page.
Kaushik
  • 2,072
  • 1
  • 23
  • 31