Reputation: 381
For a shopping cart application I'm working on for a class, I have to display information across every step. My problem is that I can display information only from the previous step. For step 1, an item is selected from a radio box. It is then stored like so:
function processStep1() {
//Implemented sessions
$_SESSION["items"] = $_POST["items"];
displayStep2();
}
Step 2 asks for a quantity and then validates it, like so:
function processStep2() {
//Implemented sessions
$_SESSION["quantity"] = $_POST["quantity"];
if ( preg_match('/^\d+$/',$_POST["quantity"]) ) {
displayStep3();
} else {
echo "ERROR: Quantity entered was invalid. Please try again.";
displayStep1();
}
}
(As an aside, I can't seem to get this to just refresh the current step (which would be displayStep2) when the input is not an integer, as I get an error every time I try to do so. If anyone has an answer for why that is, that is additionally helpful.)
But then on Step 3 of the form, I try to run the following line:
<p>You have selected <?php echo $_POST["quantity"] ?> units of the <?php echo $_POST["items"] ?>.<p>
Which responds with an error every time. I have tried various configurations of this same output, and have determined that it will always parse $_POST["quantity"], but never $_POST["items"]. I need it to do both.
Upvotes: 0
Views: 76
Reputation: 540
Your third form does not "see" $_POST['items'] because it was not submitted to that form. You stored it in a session instead. Start a new session on your third form and request $_SESSION['items'].
Step 1
<form name="step1" method="post" action="whateverPageContaintsProcessStep1.php">
<input type="radio" name="items" value="item1">Item 1
<input type="radio" name="items" value="item2">Item 2
</form>
When we submit our form the $_POST data that is going to be send will be the value of the radio button we selected we selected.
Step 2
<form name="step2" method="post" action="whateverPageContaintsProcessStep2.php">
<input type="number" name="quantity" value="0" />
</form>
When we submit form2 the $_POST data will contain the value of the textbox called quantity, however since this form does not contain the previous radio buttons this value will not be send to the server and therefor we will not be able to access it in 'whateverPageContaintsProcessStep2.php'.
Upvotes: 2