Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am working on a project which has a functionality of "featured_posts" based on this discussion. I have created a table which lists all the posts. I have a checkbox in each row which upon click submits the for through javascript. when the box is checked, it updates everything and marks the post as featured. As a test, i disabled all the featured posts. Upon unchecking the last checkbox, i got

Warning: Invalid argument supplied

warning. My code is

foreach( $_POST['list_id'] as $listID ) {
    if( update_post_meta( $listID, '_property_featured', 'true' ) ) {
        $featureMessage = "Selected listings have been marked as featured successfully.";
    }
    else $featureError = "An error occurred. Please try again.";
}

list_id refers to the post_id and was in the value attribute of checkbox. I know when the last checkbox was unchecked, i had to face this warning. Question is how can I track unchecking of checkbox as it is not included in the POST data of the form.

share|improve this question

2 Answers 2

This appears to be a general PHP warning, you're supplying an incorrect argument in a foreach loop.

$_POST['list_id'] is expected to be an array or any other (iteratable object), when no checkboxes are checked I assume $_POST['list_id'] is completely undefined.

$_POST['list_id'] = isset( $_POST['list_id'] ) ? (array) $_POST['list_id'] : array();

Adding this before you iterate over everything should help you get rid of the warning altogether.

share|improve this answer

Never assume anything about $_POST. It is user input and could contain anything. First check if $_POST['list_id'] exists, using isset() or empty(). Then make sure it is an array. You may simply cast it to an array if you wish. Finally, make sure the array values are actually absolute integers.

$ids = (isset($_POST['list_id'])) ? array_map('absint', (array) $_POST['list_id']) : array();

All post IDs not found in the submitted list should be unfeatured.

share|improve this answer
    
thanks for the suggestion. What i want is "track a checkbox click on deselect event" as an unchecked checkbox is not submitted on form submit. –  booota Apr 3 '12 at 12:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.