0

May be it would be an easy question but I did not manage it. Here is my checkboxes like this:

<input name="msg_id[]" type="checkbox" id="msg3" value="3" />
<input name="msg_id[]" type="checkbox" id="msg2" value="2" />
<input name="msg_id[]" type="checkbox" id="msg1" value="1" />
<input name="msg_id[]" type="checkbox" id="msg15" value="15" />
<input name="msg_id[]" type="checkbox" id="msg14" value="14" />

And I am posting values with $.post method like this:

$('body').on('click', '#not_read', function () {
    var inputs = $("input[name='msg_id[]']").serialize();
    $.post("user.php", {"not_read":1,"message_ids":inputs}, function(z) {$("#result").html(z)});
});

I want to send checked values to the user.php and with foreach loop I want to update reading status which checked messages. Like below..

  if (isset($_POST['not_read'])):
    if (intval($_POST['not_read']) == 0 || empty($_POST['not_read'])):
    redirect("index.php");
  endif;     

    /* I don't know what I must write here.
    $values = $_POST['message_ids'];
    foreach($values as $row):
    mysql_query("UPDATE messages SET read='0'WHERE id='".$row["id"]."'");
    endforeach;

    # Sure, it is not working...

 */
  endif;

Kind regards...

  • 2
    what does `print_r($_POST);` give you? (1:IF you tick some options 2:IF you tick none?) – Waygood May 01 '13 at 18:11
  • When I wrote print_r($_POSt), it gave me Array ( [not_read] => 1 [message_ids] => msg_id%5B%5D=4&msg_id%5B%5D=3&msg_id%5B%5D=2 ). –  May 01 '13 at 18:16
  • check this out http://stackoverflow.com/questions/4891974/jquery-serialize-for-post – Waygood May 01 '13 at 18:20
  • 1
    so in jquery use `serializeArray()` and in php `$values=json_decode($_POST['message_ids']);` – Waygood May 01 '13 at 18:22
  • Waygood, could you write foreach loop. I did not understand. –  May 01 '13 at 18:30
  • `print_r($values);` will show you the structure for you to write your processing loop. – Waygood May 02 '13 at 10:18

1 Answers1

2

If there will be more checkboxes it would be better to separate them with class names.

<input name="msg_id[]" type="checkbox" id="msg3" value="3" CLASS="msg_ids" />
<input name="msg_id[]" type="checkbox" id="msg2" value="2" CLASS="msg_ids" />
<input name="msg_id[]" type="checkbox" id="msg1" value="1" CLASS="msg_ids" />
<input name="msg_id[]" type="checkbox" id="msg15" value="15" CLASS="msg_ids" />
<input name="msg_id[]" type="checkbox" id="msg14" value="14" CLASS="msg_ids" />

if you set up data, you do not need to serialize it. Especially when you send some data without serializing.

$('body').on('click', '#not_read', function () {
    var inputs = [];
    $(".msg_ids").filter(':checked').each(function(){
        inputs.push($(this).val()); //creates array of selected boxes values
    });
    $.post("user.php", {"not_read":1,"message_ids":inputs}, function(z) {$("#result").html(z)});
});

In php, as your id's are integers, make sure they are for sql. Allso mysql_* functions are deprecated. You should use mysqli* functions.

$values = $_POST['message_ids'];
    foreach($values as $row):
    mysql_query("UPDATE messages SET read='0'WHERE id='".intval($row)."'");
    endforeach;

or you could allso do:

$values = $_POST['message_ids'];
if (is_array($values)) {
function make_safe(&$item, $key)
{
    $item = intval($item);
}
array_walk($values, 'make_safe');
if (count($values)>0) mysql_query("UPDATE messages SET read='0'WHERE id IN (".implode(', ',$values).")" );
else echo 'empty array';
}
else echo 'was not an array';
Aivar
  • 2,029
  • 20
  • 18