Jump to content

checkboxes in array check isset


UnknownPlayer

Recommended Posts

I have this code:

			$query = "SELECT * FROM boje";
		$result = mysql_query($query, $connection);
		while ($boja = mysql_fetch_array($result)) {
			echo '<input type="checkbox" name="boja[]" value="'.$boja['id'].'" />'
		}

Now problem is when i click submit, and when go through this code:

			foreach ($_POST['boja'] as $boja_id) {
			if (isset($boja_id)) {
				echo "isset";
			} else {
				echo "not isset";
			}
		}

 

this works for checkboxes which are checked, but it doesn't show echo message for checkboxes which are not checked(not isset, else code..), what is the problem, and how can i make solution for this?

Thanks..

Link to comment
https://forums.phpfreaks.com/topic/239381-checkboxes-in-array-check-isset/
Share on other sites

Instead of using the foreach to test which checkboxes were checked, what about using the while loop used to create the checkboxes again? You could loop through the database values to see which ones appear in the passed array.

 

Note that I'm not very familiar with using the array syntax for checkboxes, so I'm not sure what the code would be off hand.

Instead of using the foreach to test which checkboxes were checked, what about using the while loop used to create the checkboxes again? You could loop through the database values to see which ones appear in the passed array.

 

Note that I'm not very familiar with using the array syntax for checkboxes, so I'm not sure what the code would be off hand.

 

Something like this should do the trick

 

$query = "SELECT * FROM boje";
$result = mysql_query($query, $connection);

$boja_checked = $_POST['boja'];

while ($boja = mysql_fetch_array($result))
{
$checked = (in_array($boja['id'], $boja_checked)) ? 'checked="checked"' : '';
echo '<input type="checkbox" name="boja[]" value="'.$boja['id'].'" ' . $checked . ' />';
}

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.