2

I am using the code below in my php file to get the values from a multiple checkbox.

    if(!empty($_POST['check_list'])) {
        foreach($_POST['check_list'] as $check) {
                update_comment_meta($check, 'consider', 1);
        }


    }

The problem is that this code is apparently putting in the array $_POST['check_list'] only the checked values.

My need is to perfom the function update_comment_meta also on uncheked values, by putting '0' as the third parameter instead of '1'.

For more details, I give the code generating the HTML form:

<form action="" id="primaryPostForm" method="POST">

<?php    
         $defaults = array(
    'post_id' => $current_post); 
         $com= get_comments( $defaults );

        foreach ($com as $co) {
    if(get_comment_meta($co->comment_ID, 'consider', true)==1) {
    ?><input type="checkbox" name="check_list[]" value="<?php echo $co->comment_ID; ?>" checked="checked">

    <?php }
    else {
    ?><input type="checkbox" name="check_list[]" value="<?php echo $co->comment_ID; ?>" >
    <?php
    }}
</form>

Your usual help is always appreciated.

Adib Aroui
  • 4,981
  • 5
  • 42
  • 94

2 Answers2

7

sending unchecked value to post is somewhat not that easy.Better solution is that you name checkbox in a way using which you can easily iterate over them in post page.

Use hidden input along with checkbox.Checkbox prioritize over hidden input.

<form>
  <input type='hidden' value='0' name='check_box_con'>
  <input type='checkbox' value='1' name='check_box_con'>
</form>

Now after submit, as both have same name , check_box_con will show hidden field value if unchecked , else will override and show original.

For more see Post the checkboxes that are unchecked

Community
  • 1
  • 1
Notepad
  • 1,659
  • 1
  • 12
  • 14
  • thank you very much, I will try understand this tomorrow morning and give a feedback. – Adib Aroui Jun 20 '13 at 22:20
  • I upvoted even if it is not working. It is not the solution that worked for me. – Adib Aroui Jun 22 '13 at 12:18
  • tell me problem you are facing, in that case you can refer thread i mentioned, definitely you will get it solved – Notepad Jun 22 '13 at 20:09
  • Thank you for your time @Susheel. I was saying that my problem is resolved with another way more simple for me (I have posted it above). I upvoted your answer since it is interesting but unfortunately I didn't accept it since it didn't work for me. regards – Adib Aroui Jun 22 '13 at 22:43
1

Here is the solution I used ( based on PeeHaa comment):

        if(!empty($_POST['check_list'])) {
        foreach ($com as $co) {

        if (in_array($co->comment_ID,$_POST['check_list']))
        update_comment_meta($co->comment_ID, 'consider', 1);

        else 
         update_comment_meta($co->comment_ID, 'consider', 0);
        }
        }

In fact POST variable works like this with checkboxes, so the simple way is to use server side language to know what are values not sent via POST.

Thank you for your time.

Adib Aroui
  • 4,981
  • 5
  • 42
  • 94