dragon5006 2013-05-01 17:58
浏览 62
已采纳

jquery序列化和使用php获取值

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

  • 写回答

1条回答 默认 最新

  • douchungu0859 2013-05-01 20:30
    关注

    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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行