1

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

7
  • 1
    What is your concrete question? Commented Oct 9, 2011 at 20:45
  • My 'concrete' question, is why is it appearing as so there are no checkboxes checked and how to get 'value' stored in each of the checkboxes if they are checked. Commented Oct 9, 2011 at 20:48
  • 3
    Your code examples appear to be a bit confused. And you should not be using the deprecated font tag anymore. Commented Oct 9, 2011 at 20:54
  • 2
    How you submit the form? Is the form html valid? Have you var_dump()ed the $_POST array? Commented Oct 9, 2011 at 20:56
  • Could you show us the html your first code block generates? Could you also use var_dump($_POST) before the debugging code to see what you get? Commented Oct 9, 2011 at 21:03

3 Answers 3

1

empty($_POST['option']) will return true, if either $_POST['option'] is not set (same as !isset($_POST['option']) (!)) or an empty array.

If you need to debug what's going on, use var_dump($_POST['option']); to find out what has been submitted for the option checkboxes. I also suggest you do a var_dump($_POST); so you can see what has been submitted overall - e.g. in case the post action is not post you will immediatly notice). For HTML output:

echo '<pre>', htmlspecialchars(print_r($_POST, true)), '</pre>';

That should give you the information you're looking for. For each individual checkbox, you can do:

foreach($_POST['option'] as $option) //loop through the checkboxes
{
    var_dump($option);
}
Sign up to request clarification or add additional context in comments.

3 Comments

print_r($_POST['option']) can also be helpful. And I have never trusted empty() on arrays.
using your first line of code, it returns this: Array ( [user_id] => 20 [message_subject] => Enquiry [msg] => test [send_message] => Send )
Ensure your checkboxes are part of the form, compare with this code, you can play around with it: codepad.viper-7.com/yEvZ2r
1

First of all your code seems to be bugged to me. Maybe is just a typo but

 <input type="checkbox" name="option[]" value="$listing_id">

should be

 <input type="checkbox" name="option[]" value="<?=$listing_id?>"/>

Moreover using empty over an array is not good at all.

Comments

0

Try echoing out the $option in the loop to see what the value is and there you can see if there is something there.

foreach($_POST['option'] as $option) //loop through the checkboxes
{
    echo $option . "<br />";
}

Also make sure your form's method is set to POST or 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.

9 Comments

Nothing is echoed, even though i've checked 2 boxes.
@BenLittle Try checking the HTML with firebug or the web inspector to see if there is in fact a value in the value attribute
I've checked in the web inspector, and yes there are values in there. These are the values I want to extract when on the checked checkboxes.
Have you tried a var_dump($_POST['option']);. If so what was the results.
Using the above returns NULL.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.