2 Replies - 1125 Views - Last Post: 08 October 2012 - 11:10 AM Rate Topic: -----

#1 ettedo2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 12

problem with array and creating a dropdown box and radio button list

Posted 08 October 2012 - 09:08 AM

I create a php form that will use an associate array to populate a list box and radio buttons.
The dropbox will list colors, the radio buttons will list foods.It also will ask for First and Last Name.

I have worked on it for a while and this is my problem. I created a HTML/PHP Form that lists the drop box and radio button.Here is my Form page:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Form</title>
</head>

<body>

<form action="PAGE08.php" method="post">
<h3> Please enter your full Name and choose your favorite color and type of food</h3>
<table>
	<tr>
		<td> FirstName: </td>
		<td><input type="text" name="FirstName" /></td>
	</tr>
	<tr>
		<td>LastName: </td>
		<td><input type="text" name="LastName" /></td>
	</tr>
</table>
<div><h3> Choose your favorit color </h3></div>

<?php 
	
	$color["Rose"]="Red";
	$color["Viola"]="Blue";
	$color["Pine"]="Green";
	$color["Poppy"]="Cyan";
	$color["Lilie"]="Magenta";
	$color["Daffodil"]="Yellow";
 
	
?>
	<tr>
	<td><select name = "color" id = "color"></td>
	</tr>

<?php 
		foreach ($color as $flower => $favorite ) 
		{
		echo "<option value=\"$favorite\"> $favorite </option> \n";
		}
		
?>
</select> 
<br/><br/>
<div><h3> Choose your favorite food</h3></div>
<?php 
	
	$food["ribs"]="Barbeque";
	$food["lowmain"]="Chinese";
	$food["habatchi"]="Japanese";
	$food["padthai"]="Thai";
	$food["texas"]="Steak";
	$food["veggy"]="Vegan";
	$food["taco"]="Mexican";

		foreach ($food as $menue => $fav ) 
		{
		echo "<input type=\"radio\" name=\"radioOption\"value=\"fav\"> $fav <br/>";
		}
		
?>
<tr>
<td><input type="submit" name="button" value="Submit"/></td>
</tr>
</form>

</body>
</html>



When I click submit it gives me following error messages:

Notice: Undefined index: food in G:\xampp\htdocs\classphp\CISP1720osimonsLab008_PAGE.php on line 17

It also does not print out the selected radio button item. I does print First, Last name and color.

Here is my code for the return action page:

<!DOCTYPE HTML>
<?php
$FirstName= $_POST["FirstName"]; 										
$FirstName = trim($_POST['FirstName']); 										
$LastName = $_POST["LastName"]; 
$LastName = trim($_POST['LastName']); 
$color = ($_POST["color"]);
$food = ($_POST["food"]);
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Page</title>


<?php 
																
	$LastName = ucfirst(strtolower($LastName));														
	$FirstName = ucfirst (strtolower($FirstName));
	
	if(empty($FirstName) || empty($LastName)) 							
		{
		echo "<p><b>Incorrect Input</b></p>\n";
		echo "<p> You mist a Field. Please enter your full Name</p>\n";
		}
	else if (is_numeric($FirstName)|| is_numeric($LastName))					
		echo "<p><b>Incorrect Input</b></p>\n";
		echo "<p>You entred a numeric values</p>\n";
		}
	else if (strlen($FirstName) > 20 || strlen($LastName) > 20 )					
		{																							
		echo "<p><b>Incorrect Input</b></p>\n";
		echo "<p>First or last Name is too long.</p>\n";
		echo "<p><i>You can only use up to 20 charachters</i></p>";
		}
	else 																							
	{
	echo "<p> First Name: $FirstName </p>\n";
	echo "<p> Last Name: $LastName </p>\n";
	echo "Your Favorit color is:  ";
	print_r($color);
	echo "<br/>";
	echo "Your Favorit food is: " ;
	print_r($food);
 	}

?>

</body>
</html>



I would love to know where I am going wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: problem with array and creating a dropdown box and radio button list

#2 Lihr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-September 12

Re: problem with array and creating a dropdown box and radio button list

Posted 08 October 2012 - 09:50 AM

wouldn't you have to define it as variable $food = array(); I might be wrong.
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: problem with array and creating a dropdown box and radio button list

Posted 08 October 2012 - 11:10 AM

Ideally, yes, you should define a variable as an array before you use it. However PHP does allow you to start adding elements to an uninitialized variable as if it were an array, initializing it for you. But that opens you up to potential, unexpected side effects.

As to the problem. The radio buttons are named "radioOption" in your HTML, not "food".

This actually highlights an important mistake in your form processing code: You should always make sure form data actually exists before trying to use it. Use either the isset() or empty() functions (depending on which is more appropriate at the time) to make sure your form variables exist. - And make sure you test all of them, not just the submit button or a select few, as is far far to common.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1