Php array with checkbox form HELP!

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 form to work. I have a list of menu items which I have put into an array and I need users to be able to make their selections through a checkbox form. Once they hit submit, it is suppose to bring them to a results page listing the selections made, and return the name, item cost of each of their selection as well as the total cost. My problem is that I am able to get the form to show up abut when it is submitted, the selection does not show up.
My two scripts are suppose to function in the same way as this website link http://itm325.itmbsu.net/jstudent/Examples/Ch5/tunacafe.php but I can’t get the code to work like it.
Here’s my menu page/initial form page:
[php]

RestaurantMenu <?php include ("header.php")?>

Welcome to Sunset Bar & Grill!

<?php $menu = array('Cheeseburger','Chicken Salad','Chicken Noodle Soup', 'Supreme Pizza','Grilled Chicken Bagel','French Fries'); print 'Please select your order.
'; for($i=0;$i< count($menu);$i++) { print " $menu[$i]"; print '
'; } ?> [/php] Here's the coding for my results page. [php]!DOCTYPE html> Order Results <?php include ("header.php") ?>

Here is your order:

<?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] &#36; $item1</li>";
		}
		
		if ($item=="1") {
		$itemcost = $itemcost + $item2;
		print "<li>$menu[$item] &#36; $item2</li>";
		}
		
		if ($item=="2") {
		$itemcost = $itemcost + $item3;
		print "<li>$menu[$item] &#36; $item3</li>";
		}
		
		if ($item=="3") {
		$itemcost = $itemcost + $item4;
		print "<li>$menu[$item] &#36; $item4</li>";
		}
		if ($item=="4") {
		$itemcost = $itemcost + $item5;
		print "<li>$menu[$item] &#36; $item5</li>";
		}
		if ($item=="5") {
		$itemcost = $itemcost + $item6;
		print "<li>$menu[$item] &#36; $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>
[/php] I think its something about setting the values right but I don't know what to do right now about fixing this to work.

The value of $i is going to 0, 1, 2, etc. You want it to be the same as the selection - $menu[$i]

i am working on one for you

here is link let me know if is how you want it

http://projects1.ezewh.com/cb4.php

Thank you Cabalsdemon! Yes, it is what I wanted, except with the total included on the bottom. Sorry I took so long to reply back.

I also posted in another forum and got a response there so I haven’t check back here for awhile. How did you code this? I am still learning and would like to know the different ways to code this. Thank you again.

i named my first file cb4.php even thow it has only html its a habit
name your stuff the way you want

[php]

Cheeseburger


Chicken Salad


Chicken Noodle Soup


Supreme Pizza


Grilled Chicken Bagel


French Fries


[/php]

your basic form

then my php fom called cb.php is here

[php]

<?php foreach ($_POST['prefer'] as $a_candidate) { if (!empty($_POST['prefer'])){ if($a_candidate=="Cheeseburger"){ $item1 = "3.00"; echo "{$a_candidate} is {$item1}
"; } if($a_candidate=="Chicken Salad"){ $item1 = "2.00"; echo "{$a_candidate} is {$item1}
"; } if($a_candidate=="Chicken Noodle Soup"){ $item1 = "4.00"; echo "{$a_candidate} is {$item1}
"; } if($a_candidate=="Supreme Pizza"){ $item1 = "2.25"; echo "{$a_candidate} is {$item1}
"; } if($a_candidate=="Grilled Chicken Bagel"){ $item1 = "3.75"; echo "{$a_candidate} is {$item1}
"; } if($a_candidate=="French Fries"){ $item1 = "2.50"; echo "{$a_candidate} is {$item1}
"; } }else{ echo "Oh no! You did not order anything, yet. Please click on the Back button below to make your order again."; } } ?>

[/php]

try that and get back with me put your back button in thow

Thanks again, cabalsdemon! Yes, I got the code working on my side and see how you coded this.

Except, how would you code the total and dollar signs on this form?

do you need a entry point for peoples to say home many they want or just add up all things from the page

Thank you for the taking the time to help out. It helps a lot to better understand coding. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service