Help please!

I would like to display the name of the food item instead of the cost on the Review Order: line

Heres the line that needs to be modify i think. It shows the value, but i want it to show what food item it is

Order Review: ' . $_POST['appetizer'] .' , '. $_POST['main'] .' , '. $_POST['dessert'] .'

How would i do that? Thanks

<?php # Script 3.5 - calculator.php $page_title = 'Online Order'; include ('includes/header.html'); // Check for form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Minimal form validation: if (isset($_POST['appetizer'], $_POST['main'], $_POST['dessert']) && ($_POST['name']) && is_numeric($_POST['appetizer']) ) { // Calculate the results: $cost = $_POST['main'] + $_POST['appetizer'] + $_POST['dessert']; // Print the results: echo '

Order Submitted!

Hello, ' . $_POST['name'] . ' !

Order Review: ' . $_POST['appetizer'] .' , '. $_POST['main'] .' , '. $_POST['dessert'] .'

Pick-Up will be available in one hour.

The total cost is $ '. $cost .'

Thank you for ordering!

'; } else { // Invalid submitted values. echo '

Error!

Please enter a valid name for order pick-up.

'; } } // End of main submission IF. // Leave the PHP section and create the HTML form: ?>

Online Order

Name:

<p>Appetizer: <select name="appetizer">
	<option name="no order" value="0">No Appetizer</option>
	<option value="3">$3 Soup</option>
	<option value="3">$3 Salad</option>
	<option value="5">$5 Calamari</option>
	<option value="5">$5 Crab Cakes</option>
</select></p>
<p>Main Course: <select name="main">
	<option value="0">No Main Course</option>
	<option value="10">$10 Chicken</option>
	<option value="11">$11 Beef</option>
	<option value="12">$12 Fish</option>
	<option value="15">$15 Shrimp</option>
</select></p>
<p>Dessert: <select name="dessert">
	<option value="0">No Desert</option>
	<option value="3">$3 Pie</option>
	<option value="3">$3 Cake</option>
	<option value="4">$4 Ice Cream</option>
	<option value="5">$5 Creme Brulee</option>
</select></p>
<p><input type="submit" name="submit" value="Submit Order!" /></p>
<?php include ('includes/footer.html'); ?>

it’s partially that, but also html forms don’t pass the label only the value, so your script NEVER sees the name of the product as it is now.

change it to this:
[php]<?php

$page_title = ‘Online Order’;
include (‘includes/header.html’);

// Check for form submission:
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

// Minimal form validation:
if (isset($_POST['appetizer'], $_POST['main'], $_POST['dessert'])) {

    // Calculate the results:
    $cost = $_POST['main'] + $_POST['appetizer'] + $_POST['dessert'];

    // Print the results:

    echo '<h1>Order Submitted!</h1>
     <p><b>Hello, ' . $_POST['name'] . ' !

Order Review: ' . substr($_POST['appetizer'], (strpos($_POST['appetizer'], '-') + 1)) .' , '. substr($_POST['main'], (strpos($_POST['main'], '-') + 1)) .' , '. substr($_POST['dessert'], (strpos($_POST['dessert'], '-') + 1)). '

Pick-Up will be available in one hour.

The total cost is $ '. $cost .'

Thank you for ordering!

';
} else { // Invalid submitted values.
    echo '<h1>Error!</h1>
  <p class="error">Please enter a valid name for order pick-up.</p>';
}

} // End of main submission IF.

// Leave the PHP section and create the HTML form:
?>

Online Order

Name:

<p>Appetizer: <select name="appetizer">
        <option name="no order" value="0">No Appetizer</option>
        <option value="3-Soup">$3 Soup</option>
        <option value="3-Salad">$3 Salad</option>
        <option value="5-Calamari">$5 Calamari</option>
        <option value="5-Crab Cakes">$5 Crab Cakes</option>
    </select></p>
<p>Main Course: <select name="main">
        <option value="0">No Main Course</option>
        <option value="10-Chicken">$10 Chicken</option>
        <option value="11-Beef">$11 Beef</option>
        <option value="12-Fish">$12 Fish</option>
        <option value="15-Shrimp">$15 Shrimp</option>
    </select></p>
<p>Dessert: <select name="dessert">
        <option value="0">No Desert</option>
        <option value="3-Pie">$3 Pie</option>
        <option value="3-Cake">$3 Cake</option>
        <option value="4-Ice Cream">$4 Ice Cream</option>
        <option value="5-Creme Brulee">$5 Creme Brulee</option>
    </select></p>
<p><input type="submit" name="submit" value="Submit Order!" /></p>
[/php]

you’ll notice i changed the form values to include the name:
[php] No Desert
$3 Pie
$3 Cake
$4 Ice Cream
$5 Creme Brulee[/php]

I also removed the check to make sure the values are numeric, though i recommend some better validation and not use the $_POST values directly.

if you want this line:

Order Review: Soup , Chicken , Ice Cream
to look like this:
Order Review: $3 Soup , $10 Chicken , $4 Ice Cream
change this line in the code: [php]

Order Review: ' . substr($_POST['appetizer'], (strpos($_POST['appetizer'], '-') + 1)) .' , '. substr($_POST['main'], (strpos($_POST['main'], '-') + 1)) .' , '. substr($_POST['dessert'], (strpos($_POST['dessert'], '-') + 1)). '[/php]

to this:
[php]

Order Review: $' . str_replace('-', ' ', $_POST['appetizer']) .' , $'. str_replace('-', ' ', $_POST['main']) .' , $'. str_replace('-', ' ', $_POST['dessert']) .'[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service