Jump to content

Checkboxes and radio boxes not working right i think


flashpointdevon

Recommended Posts

Im using chrome.

 

Shouldt these have a 1 value only if checked? I tried without assigning value and with, I cant get it to work right. Same for radio boxes. I tried googling this and to my surprise I could not find anything relevant. How do I get a 0 for not checked and a 1 for checked? Thanks in advance im sure someone knows this

 

				<input type="checkbox" name="test_clean" id="test_clean" value="1" />
			<label for="test_clean">Test Clean</label>
			<input type="checkbox" name="test_works" id="test_works" value="1" />
			<label for="test_works">Test Works</label>
			<input type="checkbox" name="test_parts" id="test_parts" value="1" />
			<label for="test_parts">Test Parts</label>
			<input type="checkbox" name="test_volume" id="test_volume" value="1" />
			<label for="test_volume">Test Volume</label>
			<input type="checkbox" name="test_damage" id="test_damage" value="1" />
			<label for="test_damage">Test Damage</label>
			<input type="checkbox" name="shipped" id="shipped" value="1" />
			<label for="shipped">Shipped</label>

How do I get a 0 for not checked and a 1 for checked?

By starting with a 0 for every possible checkbox and then setting each to 1 if they've been submitted. That's because unchecked boxes are not sent to PHP.

$checkboxes = array(
"test_clean" => 0,
"test_works" => 0,
"test_parts" => 0,
"test_volume" => 0,
"test_damage" => 0,
"shipped" => 0
);

foreach ($checkboxes as $key => &$value) {
if (!empty($_POST[$key])) $value = $_POST[$key];
}
unset($value);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.