Sending PHP variables between pages

I created a registration form that successfully transfers its variables from the registration page (go-gold.php) to the final / verfication page (go-gold-summary.php). On the second page, the data is displayed correctly.

However, I want to use the image button to return to the registration page if the user made an input error. Returning, the original form should now be filled with the data that was first entered.

The problem is that I cannot resend / return data from the second page, back to the first. My text fields are displayed 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!

+7
source share
3 answers

go-gold-summary.php should be changed as follows.

 <?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 changed this line

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

in that

 <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 as follows:

 $_POST["index"] = value; 

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

You missed this in your code. Just update it with my code and it will work

+2
source

Why don't you want to use php session? Please provide a reason not to use it. I ask about this, as my reputation does not allow me to comment on questions or answer any questions other than my own. For this, Plese does not do this.

Another way might be to use cookies to temporarily store data, but this and the placement of data back and forth in a mail request is really unsafe compared to a session.

+1
source

There are very few ways to maintain variables between pages. An alternative is to have a separate form on the second page with hidden text fields containing $ _POST data, and the submit button calls up the previous page. It is impossible to bypass the "back button" in the browser, unfortunately.

I missed the bold text about session variables - ignore it if not applicable:

one way to store variables on server-side pages is to use $ _SESSION

first include the following at the top of your PHP pages to keep the session active:

 session_start(); 

after submitting the application and going to page 2, add the following:

 $_SESSION['customer_name'] = $_POST['customer_name']; 

In addition, on the first page, you can change the form element as such:

 <input type="text" name="customer_name" value="<?PHP if isset($_SESSION['customer_name'] || !empty($_SESSION['customer_name'])) { echo $_SESSION['customer_name']; } ?>"> 

this will save the completed data and display it when the user returns the whole page, and if they add something else, it will be updated when they return to page 2.

0
source

All Articles