Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending PHP variables back and forth between pages

I've created a registration form that successfully passes its variables from the registration page (go-gold.php) to a summary/verfication page (go-gold-summary.php). The data appears correctly on the second page.

However, I want to able to use an image button to return back to the registration page, in case the user made an entry error. Going back, the original form should now be populated with the data that was first entered.

The problem is that I cannot re-send/return the data from the second page, back to the first. My text fields appear blank. I do NOT want to use Session variables.

The code is truncated from the entire page.

Registration Page (go-gold.php):

<?php
$customer_name = $_POST['customer_name'];
?>

<form action="go-gold-summary.php" method="post">

Name: <input type="text" name="customer_name" id="customer_name" value= "<?php echo $customer_name ?>" />
<input name="<?php echo $customer_name ?>" type="hidden" id="<?php echo $customer_name ?>">

</form>

Summary Page (go-gold-summary.php)

<?php
$customer_name = $_POST['customer_name'];
?>

<form action="go-gold.php" method="post">

Name: <?php echo $customer_name ?> <input type="hidden" id="<?php echo $customer_name ?>" name="<?php echo $customer_name ?>">

<INPUT TYPE="image" src="images/arrow_back.png" id="arrow" alt="Back to Registration"> (Button to go back to Registration Page)

</form>

Thanks!

like image 300
MacGyver_97 Avatar asked Apr 23 '13 02:04

MacGyver_97


People also ask

How can we pass the variable through the navigation between the pages?

Three ways can pass the variable through the navigation between the pages: Put the variable into session in the first page, and get it back from session in the next page. Put the variable into cookie in the first page, and get it back from the cookie in the next page.


2 Answers

go-gold-summary.php should be changed like this.

<?php
$customer_name = $_POST['customer_name'];
?>

<form action="go-gold.php" method="post">

Name: <?php echo $customer_name ?> <input type="hidden" value="<?php echo $customer_name ?>" name="customer_name">

<INPUT TYPE="submit" src="images/arrow_back.png" id="arrow" alt="Back to Registration"> (Button to go back to Registration Page)

</form>

notice how I've changed this line

<input type="hidden" id="<?php echo $customer_name ?>" name="<?php echo $customer_name ?>">

into this

<input type="hidden" value="<?php echo $customer_name ?>" name="customer_name">

$_POST is an associative array and as you submit the form it will be populated like this:

$_POST["index"] = value;

where "index" is the text field "name" and value is the text field value.

You've missed that one in your code. Just update it with my code and it will work

like image 162
Saturnix Avatar answered Sep 21 '22 12:09

Saturnix


Why you would not want to use the php session? Please give any reason for not to use it. I am asking this way since my reputation does not allow me to comment questions or answers any other than my own. Plese do not -1 for this.

Another way could be using cookies to store the data temporarily, but that and posting the data back and forth in the post request is really insecure compared to session.

like image 29
Lasse Avatar answered Sep 22 '22 12:09

Lasse