1

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.

2 Answers 2

0

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.

0

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.

1
  • 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, 2012 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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