I am currently trying to design a for loop to iterate through any ‘checked’ check boxes, and from that, use the value within the ‘value’ slot to run some queries. I am unfortunately struggling with this as the list of checkboxes are not pre-defined, they are dynamic from the database pending the users previous selection.
The loop actually works to present the items to be checked:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
The listing ID within the value is what I need to work with in a mysql query before I run an update query. The for loop that’s meant to work is:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
The update query will work within this as its simply copied from somewhere else, I just need the ‘Listing_ID’ from the check boxes that are checked.
I ran this code to hopefully do some debugging:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
and it returns “no checkboxes checked.”
I have now hit a grey area as to why this for loop isn’t working (this was taken from another example on the internet).
Try echoing out the
$optionin the loop to see what the value is and there you can see if there is something there.Also make sure your form’s method is set to
POSTor that it’s action is pointed to the correct place. You also have an error in your input:<input type="checkbox" name="option[]" value="$listing_id">I assume you meant:
<input type="checkbox" name="option[]" value="<?php echo $listing_id;?>">UPDATE:
The error ended up not being in the code posted. Error was discovered in an if statement that always returned false that in-cased the code posted above.