I’ve been stuck on this problem for a while now and seeking some help. Thanks in advanced. I have a form on a page (guest/build.php) with a bunch of radio buttons:
<form action="../php/build.php" method="POST">
<table class="table">
<caption>Your Computer Parts</caption>
<tr><th>General Component</th><th>Specific Component</th><th>Cost</th></tr>
<?php include('buildFunction.php');?>
</table>
<input id="buyMe" type="submit" value="BUY ME NOW!!!">
</form>
This sends the data from the radio buttons to my form handler(php/build.php) which makes sure everything has been selected:
<?php
include('manageUser.php');
$mainCheck = isset($_POST["chasis"]) && isset($_POST["cpu"]) && isset($_POST["mobo"]) && isset($_POST["ram"]) && isset($_POST["gpu"]) && isset($_POST["psu"]) && isset($_POST["hdd"]) && isset($_POST["monitor"]) && isset($_POST["keyboard"]) && isset($_POST["mouse"]);
if(!$mainCheck){
redirect("../guest/build.php","Please select 1 of each
component");
} else {
redirect("../user/purchase.php","Items Successfully Added");
}
?>
And my redirect function (php/manageUser.php):
function redirect($url, $message){
session_start();
$_SESSION["message"] = $message;
//var_dump($_POST);
//exit;
header("Location: $url");
die;
OK, so the results of that commented var_dump are exactly what I want:
array(10) { ["chasis"]=> string(1) "3" ["cpu"]=> string(1) "9" ["mobo"]=> string(2) "13" ["ram"]=> string(2) "18" ["gpu"]=> string(2) "22" ["psu"]=> string(2) "27" ["hdd"]=> string(2) "31" ["monitor"]=> string(2) "38" ["keyboard"]=> string(2) "42" ["mouse"]=> string(2) "47" }
}
However, as soon as I call the header function, I loose the data in the POST array. I tried saving it into a global variable, but it always registered NULL, and didn’t seem proper anyway.
My goal is that the ID’s of each item stay in the POST array after the redirect, which I can then use to fill out a price table on the next page(user/purchase.php).
Am I going about this the wrong way? And if so what is the proper way? I’m trying to stick with PHP at this phase and not use javascript here.
I could come up with two solutions if you don’t want to modify the flow of your code:
Store the values of all selections in session
Append your build.php like build.php?chasis=bla&cpu=blabla, i.e., store the previous selections in $_GET. You could decide how you want to do it in case the uri gets too long, but you get the idea.