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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
From your code snippet, no.

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

HTH,
Dan
0
Dave BaldwinFixer 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
Olaf DoschkeSoftware 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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
NerdsOfTechTechnology 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
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 DoschkeSoftware 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.