Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDarrel Lyons
2,991 PointsArrays with $_POST
I'm practicing arrays but i'm having problems: p.s. I'm new to PHP. I've done 8 array inside arrays for 8 different cars showing the name of the cars , the price and the picture. I can identify them all differently before i post but when i click one the submit buttons, it doesn't echo or purchase the car that was purchased. If i change the POST to if ($_POST[101];) and purchase the first car, it will automatically purchase the last car instead.
$vehicles = array();
$vehicles[101] = array(
"car" => "Mobility Scooter",
"price" => "4000",
"img" => ""
);
$vehicles[102] = array(
"car" => "Reliant Robin",
"price" => "20000",
"img" => ""
);
<form method="post" name="form1" id="form1">
<?php foreach($vehicles as $vehicle_id => $vehicle) { ?>
<p><?php echo $vehicle['car'];?>
<input name="<?php echo $vehicle_id; ?>" type="submit" class="custombutton" id="clear" value="Purchase Car"></p>
<?php } ?>
</form>
</div>
<?php
if ($_POST['$vehicle_id']){
echo "The " . $vehicle['car'] . " is now in your garage!";
}
8 Answers
Jeff Busch
19,287 PointsHi Darrel,
If you haven't already solved your problem this might help. In a real world application there's more you would add but this is a start.
Jeff
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<title>Test php</title>
<style>
body {
background-color: #D0AD7F;
}
#main-wrapper {
width: 50%;
margin: 1% auto;
padding: 1%;
text-align: center;
border: 1px solid #951695;
position: relative;
}
</style>
</head>
<body>
<div id="main-wrapper">
<h3>Darrel Lyons Used Cars</h3>
<?php
$carPicked = $_POST["carPicked"];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
$vehicles = array();
$vehicles[101] = array(
"car" => "Mobility Scooter",
"price" => "4000",
"img" => ""
);
$vehicles[102] = array(
"car" => "Reliant Robin",
"price" => "20000",
"img" => ""
);
$vehicles[103] = array(
"car" => "Plymoth Valiant",
"price" => "125",
"img" => ""
);
?>
<form method="post" name="form1" id="form1" action="<?php echo $PHP_SELF; ?>">
<?php foreach($vehicles as $vehicle_id => $vehicle) { ?>
<p><?php echo $vehicle['car'];?>
<input name="carPicked" type="radio" class="custombutton" id="clear" value="<?php echo $vehicle['car']; ?>"></p>
<?php } ?>
<input type="submit" value="Purchase Car" name="submit">
</form>
<?php } else {
echo "The " . $carPicked . " is now in your garage!<br>";
echo "<a href='test-2.php'>Back to form</a><br>";
echo "<hr>";
} ?>
</div>
</body>
</html>
Benjamin Bornschein
1,364 PointsYou did a little syntax mistake.
When you use $_POST and want to access a key, you use ' e.g. $_POST['my_key'];
But when the name of a key is stored in a variable, you just use $_POST[$myVariable].
So just fix your if-statement: if ($_POST[$vehicle_id]) {
Btw. the way you want to select a specific vehicle isn't not a good way.
Instead of using a submit input, use a radio.
E.g.
<form method="post" name="form1" id="form1">
<?php foreach($vehicles as $vehicle_id => $vehicle) { ?>
<p><?php echo $vehicle['car'];?>
<input name="selectedVehicle" type="radio" value="<?php echo $vehicle_id; ?>"></p>
<?php } ?>
<input type="submit" value="Purchase car" class="custombutton" id="clear" />
</form>
</div>
Now you can access the selected vehicle id
if (!empty($_POST['selectedVehicle']) && !empty($vehicle[$_POST['selectedVehicle']])){
echo "The " . $vehicles[$_POST['selectedVehicle']]['car'] . " is now in your garage!";
}
Darrel Lyons
2,991 PointsIt works, thank you very much!! :) It only works if i put the PHP below the HTML form but then it echos below the HTML. Is there any way for this to work with the PHP above the HTML?
Benjamin Bornschein
1,364 PointsSure. Just put this code above your HTML-Structure.
<?php
$vehicles = array();
$vehicles[101] = array(
"car" => "Mobility Scooter",
"price" => "4000",
"img" => ""
);
$vehicles[102] = array(
"car" => "Reliant Robin",
"price" => "20000",
"img" => ""
);
if (!empty($_POST['selectedVehicle']) && !empty($vehicle[$_POST['selectedVehicle']])){
echo "The " . $vehicles[$_POST['selectedVehicle']]['car'] . " is now in your garage!";
}
?>
<form method="post" name="form1" id="form1">
<?php foreach($vehicles as $vehicle_id => $vehicle) { ?>
<p><?php echo $vehicle['car'];?>
<input name="selectedVehicle" type="radio" value="<?php echo $vehicle_id; ?>"></p>
<?php } ?>
<input type="submit" value="Purchase car" class="custombutton" id="clear" />
</form>
Darrel Lyons
2,991 PointsWhen i do this, the echo comes out as "The is now in your garage!"
Benjamin Bornschein
1,364 PointsThen you didn't used my if-statement ;-) As long as the form isn't submitted, there is no $_POST['selectedVehicle'] key in $_POST and the if-statement is "false".
Darrel Lyons
2,991 PointsI'm using:
if (!empty($_POST['selectedVehicle'])){
Because this one wasn't working:
!empty($vehicle[$_POST['selectedVehicle']])
Benjamin Bornschein
1,364 PointsCan you please upload your full code for review?
Darrel Lyons
2,991 PointsHere it is -- >> http://www.sendspace.com/file/olhqx7
Benjamin Bornschein
1,364 PointsYour if-statement is correct, there should no "The is now in your garage" displayed. Is it possible, that you press the refresh button on your website and re-submit the form?
Darrel Lyons
2,991 PointsNo, i'm not refreshing. When i put the PHP underneath the HTML, it works fine but it echos underneath the HTML.
Benjamin Bornschein
1,364 PointsAh sorry, know I understand your issue.
You have to clean your code a little bit. Store your messages in a message-variable, e.g. $messages.
Instead of echoing, store the message in an array.
// not completed coded, just an example
$messages = array();
if (!empty($_POST['selectedVehicle'])){
if($fetch->money < 4000){
$messages[] = "<font size='1'>You need more money to purchase this car!<br><br>";
} elseif ($fetch->money > 4000){
$messages[] = "<center><font size='1'>The " . $motor_vehicle . " is now in your garage!<br><br></center>";
}
}
And now write this near your form
<body>
<?php
if (!empty($messages)) {
foreach ($messages as $value) {
echo $value;
}
}
?>
<div>
<form method="post" name="form1" id="form1">
Darrel Lyons
2,991 PointsIt still isn't working for me. Should i change the $_POST to a $_POST. I'm sorry for wasting your time here.
Benjamin Bornschein
1,364 PointsHmm, DirectMessage me via Twitter @BenBornschein
Darrel Lyons
2,991 PointsDarrel Lyons
2,991 PointsThank you Jeff!! :)