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!
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
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.
there are very few ways to maintain variables across pages. The alternative is to have separate form on the second page with hidden text fields containing the $_POST data, and the submit button calls the previous page. No way of getting around the "back button" on a browser though unfortunately.
I missed the bold text about the session variables - disregard if this does not apply:
one way to maintain variables across pages on the server side is to use $_SESSION
first include the following at the top of your PHP pages to keep a session active:
session_start();
once you submit the for and move to page 2, add the following:
$_SESSION['customer_name'] = $_POST['customer_name'];
As well, on the first page, you could 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 keep the filled in data and display it when the user returns tot he page, and if they put in something different it will be updated when they hit page 2 again.
链接地址: http://www.djcxy.com/p/12902.html下一篇: 在页面之间来回发送PHP变量