Get values from HTML to PHP

HTML to get check box in PHP
I've been sick for the last 2 days.  This has to be an easy one that I can't see.
Keyiin Page:
<!-- Food -->   
 		<td width = 10%>
			<div id="Body_Labels">
	<div id="Body_Labels">
				<input type="checkbox" name="Food" value=" <?php echo $Food ?>"> Food
			</div>
		</td>
		<!-- End Food -->        
      <!-- Vent -->   
 		<td width = 10%>
			<div id="Body_Labels">
				<input type="checkbox" name="Vent" value=" <?php echo $Vent ?>"> Vent
			</div>
		</td>
		<!-- End Vent --> 
               
        <!-- Corner -->   
 		<td width = 10%>
			<div id="Body_Labels">
				<input type="checkbox" name="Corner" value=" <?php echo $Corner ?>"> Corner
			</div>
		</td>

Open in new window

Search Page:
		echo "food = ".$_POST['Food']."<br>";
		echo "vent = ".$_POST['Vent']."<br>";
		echo "corner = ".$_POST['Corner']."<br>";

Open in new window


Shouldn't this work?
breeze351Asked:
Who is Participating?
 
NerdsOfTechConnect With a Mentor Technology ScientistCommented:
You have to check isset() on $_POST elements before outputing or you'll get an undefined index error.

		echo "food = ". isset($_POST['Food'])? $_POST['Food'] : '' . "<br>";
		echo "vent = " . isset($_POST['Vent'])? $_POST['Vent'] : '' . "<br>";
		echo "corner = " . isset($_POST['Corner'])? $_POST['Corner'] : '' . "<br>";

Open in new window

1
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
From your code snippet, no.

It's missing a <form method="post"> somewhere before the checkboxes.

HTH,
Dan
0
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
Also missing a 'submit' input or button at the end before an ending </form> tag.  Also some HTML errors.  'id's are supposed to be unique.  I changed them to 'class'.

<form method="post">
<table><tr>
<!-- Food -->   
 		<td width = 10%>
			<div class="Body_Labels">
				<input type="checkbox" name="Food" value=" <?php echo $Food ?>"> Food
			</div>
		</td>
		<!-- End Food -->        
      <!-- Vent -->   
 		<td width = 10%>
			<div class="Body_Labels">
				<input type="checkbox" name="Vent" value=" <?php echo $Vent ?>"> Vent
			</div>
		</td>
		<!-- End Vent --> 
               
        <!-- Corner -->   
 		<td width = 10%>
			<div class="Body_Labels">
				<input type="checkbox" name="Corner" value=" <?php echo $Corner ?>"> Corner
			</div>
		</td>
</tr>
<tr><td><input type="submit" name="submit" value="Submit">
</tr></table>
</form>

Open in new window

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Olaf DoschkeConnect With a Mentor Software DeveloperCommented:
Besides all that, whether an input type is rendered checked or not has to be set by adding the boolean checked attribute (if you don't know, a boolean attribute is not in the form attribute="value" but is added - meaning true or not added - meaning false - to the html tag)

If you try to set the status via setting the value to the variables $Food, $Vent, and $Corner, the value only sets what's coming back to PHP in the corresponding $_POST variable, if the checkbox is checked when submitting.

When the Food checkbox is checked, $_POST['Food'] will be set and will be whatever PHPs $Food was, when your PHP creates this HTML input tag. In case the checkbox is not set $_POST['Food'] will not be an empty value or false, it will not be set at all.

Bye, Olaf.
0
 
breeze351Author Commented:
I'm sorry.  Like I said I'm fighting some kind of bug
I've attached both the keyin page and the display page.

If I check "vent" I get the following error:
Notice: Undefined index: Food in /home/mrbreez1/public_html/test/Survey_Display.php on line 22
food =
vent = Y

Notice: Undefined index: Corner in /home/mrbreez1/public_html/test/Survey_Display.php on line 24
corner =
food =
vent = Y
corner =

Thanks
Glenn
Space_Survey.php
Survey_Display.php
0
 
Chris StanyonConnect With a Mentor Commented:
Checkboxes and Radio buttons only get sent to the server if they're checked / ticked. If they're left unticked, then the data won't be sent, so the POST key won't exist. That's the point NerdsOfTech is making. You need to check whether it was submitted (checked) by using the isset() method.

A general way to deal with this is to come up with a value that indicates it isn't ticked, such as "n/a", "Not Ticked", "No", and run a ternary operator against the isset method:

$foodOption = isset($_POST['Food']) ? $_POST['Food'] : "Not Checked";

Now the $foodOption variable will contain the value of the tickbox if it was ticked and the value of "Not Checked" if it wasn't.
1
 
Olaf DoschkeConnect With a Mentor Software DeveloperCommented:
It's not a bug, that's the way this HTML input type works, you don't have an empty $_POST['Food'] as feedback from the HTML form, this $_POST variable index isn't set if the checkbox isn't set. I already said so:
In case the checkbox is not set $_POST['Food'] will not be an empty value or false, it will not be set at all.
In consequence, you have to check the existence of the $POST array index 'Food' using isset().

Notice $_POST is an array and in detail not a simple array with numerically indexed elements but an associative array. Nevertheless, the term "index" is used for what you specify as element index, eg 'Food' in case of $_POST['Food']. Using an array with an undefined index results in that error message. You expect it to be defined as you defined an input with Name='Food', but in case of input type checkbox that's not always creating such an element of $_POST, only if the checkbox is checked. And this is normal, not a bug, you just have to react accordingly.

Bye, Olaf.
1
 
NerdsOfTechTechnology ScientistCommented:
Many issues were resolved with the help of experts. As such, I vote to split the points evenly: 200 points a piece. It looks like the main issue ended up being undefined index issues; checking isset() on a $_POST element to avoid outputting a non-existent element solves this.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.