Sarah Collin
Sarah Collin

Reputation: 27

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

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

Upvotes: 1

Views: 14944

Answers (5)

Ashwini Agarwal
Ashwini Agarwal

Reputation: 4858

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']);
    

Upvotes: 1

Kaushik
Kaushik

Reputation: 2090

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.

Upvotes: 0

Krish R
Krish R

Reputation: 22711

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.

Upvotes: 0

Vainglory07
Vainglory07

Reputation: 5273

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">';
}

Upvotes: 0

Jenz
Jenz

Reputation: 8369

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>

Upvotes: 2

Related Questions