Avatar of MarcPerez21
MarcPerez21

asked on 

Using PHP to view array from a post

This is something i'm reading out of textbook, and i'm learning about php and i'm at the array chapter. i can't seem to get the value prefer[] in my tunaResult.php

I don't know why i get these:
Notice: Undefined variable: prefer in C:\EasyPHP\www\webWizard\chapter5\tunaResults.php on line 10
Notice: Undefined index: in C:\EasyPHP\www\webWizard\chapter5\tunaResults.php on line 10
You don't like our food?

Why i can't view the array of the items that were selected from the previous form.
// tunaSurvey.php
<html>
<head>
	<title>Tuna Cafe</title>
</head>
<body>
	<font size="4" color="blue">Welcome to the Tuna Cafe Survey!</font>
	
	<form action="tunaResults.php" method="POST">
		<?php
			$menu = array('Tuna Casserole', 'Tuna Sandwich', 'Tuna Pie', 'Grilled Tuna', 'Tuna Surprise');
			$bestSeller = 2;
			print 'Please indicate all your favorite dishes.<br />';
			for ($i=0; $i < count($menu); $i++)
			{
				print "<input type=\"checkbox\" name=\"prefer[]\" value=\"$i\"> $menu[$i]";
				if ($i == $bestSeller)
				{
					print '<font color="red">Our best Seller!!!</font>';
				}
				print '<br/>';
			}
		?>
		<input type="submit" value="Click to submit">
		<input type="reset" value="Erase and Restart">
	</form>
</body>
</html>
 
//tunaResults.php
<html>
	<head>
		<title>Tuna Cafe</title>
	</head>
	<body>
		<font size="4" color="blue">Tuna Cafe Results received</font>
		<?php
			
			$menu = array('Tuna Casserole', 'Tuna Sandwich', 'Tuna Pie', 'Grilled Tuna', 'Tuna Surprise');
			if (count($_POST["$prefer"]) != 0){
				print '<br />Your selection were <ul>';
				foreach ($prefer as $item){
					print "<li>$menu[$item]</li>";
				}
				
			} else {
				print "You don't like our food?";	
			}
			print '</ul>';
		?>
	</body>
</html>

Open in new window

PHP

Avatar of undefined
Last Comment
MarcPerez21

8/22/2022 - Mon