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

Link to comment
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.

 

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 . ' />';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.