Jump to content

Need Coding Advice On This Menu Array Checklist Form Script


juxmaichong

Recommended Posts

Hi! I am just learning php and i don't think i know what I'm doing here. Any explanation or advice would be greatly appreciated.

 

I need a checklist form to work. I have a list of menu items which I have put into an array. Users should be able to select their food item to order and once submitted, it is suppose to bring them to a results page listing the name of selections made, item cost of each selection, as well as the total cost. I am supposed to use conditional statements to find this but I'm not sure if I am coding this right.

 

My problem is that I am able to get the form to show up but when it is submitted, none of the selected items show up.

My two scripts are suppose to function in the same way as the sample on this website link http://itm325.itmbsu...h5/tunacafe.php but I can't get the code to work like that

.

Here's my menu page/initial form page:

<!DOCTYPE html>
<html>
<head>
<title>RestaurantMenu</title>
</head>
<body>
<?php include ("header.php")?>
<font size= "5" color ="blue">
 <h1>Welcome to Sunset Bar & Grill!</h1>
</font>
<form action ="orderResults.php" method="post">
 <?php
  $menu = array('Cheeseburger','Chicken Salad','Chicken Noodle Soup',
  'Supreme Pizza','Grilled Chicken Bagel','French Fries');
  print 'Please select your order. <br>';
  for($i=0;$i< count($menu);$i++) {
   print "<input type=\"checkbox\" name=\"prefer[]\" value= $i> $menu[$i]";
   print '<br>';
  }
 ?>
 <input type="submit" value="Click to Submit">
 <input type="reset" value="Erase and Restart">
</form>
</body>
</html>

 

Here's the Results coding page.


<!DOCTYPE html>
<html>
<head>
<title>Order Results</title>
</head>
<body>
<font size= "5" color ="blue">
<h1>Here is your order:</h1>
</font>
<?php
$prefer= $_post['prefer'];
$menu = array('Cheeseburger', 'Chicken Salad', 'Chicken Noodle Soup',
'Supreme Pizza', 'Grilled Chicken Bagel', 'French Fries');
if (count($prefer) ==0){
print "Oh no! You did not order anything, yet. Please click on the Back button below to make your order again.";
}

else {
print "<h1>Your selections were</h1>
<ul>";
$item1 = "3.00";
$item2 = "2.00";
$item3 = "4.00";
$item4 = "2.25";
$item5= "3.75";
$item6= "2.50";
}

foreach ($prefer as $item) {
if ($item=="0") {
$itemcost = $itemcost + $item1;
print "<li>$menu[$item] $ $item1</li>";
}

if ($item=="1") {
$itemcost = $itemcost + $item2;
print "<li>$menu[$item] $ $item2</li>";
}

if ($item=="2") {
$itemcost = $itemcost + $item3;
print "<li>$menu[$item] $ $item3</li>";
}

if ($item=="3") {
$itemcost = $itemcost + $item4;
print "<li>$menu[$item] $ $item4</li>";
}
if ($item=="4") {
$itemcost = $itemcost + $item5;
print "<li>$menu[$item] $ $item5</li>";
}
if ($item=="5") {
$itemcost = $itemcost + $item6;
print "<li>$menu[$item] $ $item6</li>";
}

}
print "</ul>";
print 'Thank you! Your order will be ready soon.';
?>
<form method="link" action="menu.php">
<input type="submit" value="Back to Order">
</form>
</body>
</html>

 

PhpHelp.zip

 

I would appreciate any advice or information to help me out.

Edited by juxmaichong
Link to comment
Share on other sites

Main problem is $_post needs to be $_POST.

 

You should also use a loop to process your selection eg.

 

<!DOCTYPE html>
<html>
<head>
<title>Order Results</title>
</head>
<body>
<font size= "5" color ="blue">
<h1>Here is your order:</h1>
</font>
<?php
$prefer= $_POST['prefer'];			 // $_POST must be uppercase
$menu = array('Cheeseburger', 'Chicken Salad', 'Chicken Noodle Soup',
'Supreme Pizza', 'Grilled Chicken Bagel', 'French Fries');
$prices = array (3.00, 2.00, 4.00, 2.25, 3.75, 2.50);	 // prices with same keys as menu array
if (count($prefer) ==0){
   print "Oh no! You did not order anything, yet. Please click on the Back button below to make your order again.";
}
else {
   print "<h1>Your selections were</h1>
   <ul>";
   $itemcost = 0;
   foreach ($prefer as $item) {
	 printf('<li>%s $ %0.2f</li>', $menu[$item], $prices[$item]);
	 $itemcost += $prices[$item];
   }
   printf ("</ul><br/>Total cost: %0.2f<br/> ", $itemcost);
   print 'Thank you! Your order will be ready soon.';
}
?>
<form method="get" action="menu.php">
<input type="submit" value="Back to Order">
</form>
</body>
</html>

Link to comment
Share on other sites

Main problem is $_post needs to be $_POST.

 

You should also use a loop to process your selection eg.

 

<!DOCTYPE html>
<html>
<head>
<title>Order Results</title>
</head>
<body>
<font size= "5" color ="blue">
<h1>Here is your order:</h1>
</font>
<?php
$prefer= $_POST['prefer'];			 // $_POST must be uppercase
$menu = array('Cheeseburger', 'Chicken Salad', 'Chicken Noodle Soup',
'Supreme Pizza', 'Grilled Chicken Bagel', 'French Fries');
$prices = array (3.00, 2.00, 4.00, 2.25, 3.75, 2.50);	 // prices with same keys as menu array
if (count($prefer) ==0){
print "Oh no! You did not order anything, yet. Please click on the Back button below to make your order again.";
}
else {
print "<h1>Your selections were</h1>
<ul>";
$itemcost = 0;
foreach ($prefer as $item) {
	 printf('<li>%s $ %0.2f</li>', $menu[$item], $prices[$item]);
	 $itemcost += $prices[$item];
}
printf ("</ul><br/>Total cost: %0.2f<br/> ", $itemcost);
print 'Thank you! Your order will be ready soon.';
}
?>
<form method="get" action="menu.php">
<input type="submit" value="Back to Order">
</form>
</body>
</html>

 

It actually works now and you are a life saver! Thank you so much, Barand! Looks like I was coding this wrong in my second page. I greatly appreciate your time in fixing this code for me. Have a great day! :happy-04:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.