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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失