dtp87205 2012-02-16 22:14
浏览 82
已采纳

如何通过POST AJAX将JS数组传递给PHP?

Here is my code:

var var_ids = new Array();
var i = 0;
jQuery("select").each(function() {
var_ids[i] = jQuery("option:selected",this).val();
i++;
}

var $data = {
action: "do_something",
var_ids: var_ids,
};
jQuery.post(doajax.ajaxurl, $data, function(response) {
    alert(response);

}); 

And in my php:

$ids = mysql_real_escape_string(trim($_POST['var_ids']));
exit(print_r($ids));

And it just returns garbage...So my question is how to pass an array via AJAX to post php?

Thank you.

  • 写回答

6条回答 默认 最新

  • dragonfly9527 2012-02-16 22:40
    关注

    Most likely you get unexpected results because you apply string-related functions trim and mysql_real_escape_string to array $_POST['var_ids']

    As long as it is just an array of integers - the only mysql sanitize you need is casting to int:

    $ids = array_map('intval', $_POST['var_ids']);
    print_r($ids);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器