dtbi27903 2016-11-18 21:10
浏览 48
已采纳

jQuery AJAX发送搜索条件到PHP返回JSON

I am trying to send a group of variables to a PHP script via AJAX.

Typically, this is what I'd do:

 $('.submitSearch').on('click', function()
 {
   var rep = $('#rep').val();
   var num = $('#num').val();
   var uid = $('#uid').val();
   // and so on
   // then I could send each variable to a PHP script
   $.post('api/summary.php', {rep: rep, num: num, // and so...}, function(data)
   {
     console.log(data);
   });
 });

That's how I'd normally do it.

But now, I am trying send all of the parameters in a single variable I calling searchCriteria, as follows:

 $('.submitSearch').on('click', function()
 {
   var searchCriteria = 
   {
     rep: $('#rep').val(),
     num: $('#num').val(),
     uid: $('#uid').val(),
     // and so on...
   }

   // then send them to the php script

   $.post('api/summary.php', searchCriteria, function(data)
   {
     console.log(data);
   });
 });

Then, in the PHP script, retrieve all of the parameters from the variable for processing:

 <?php
  if($_POST['searchCriteria'] == true)
  {
    // get the parameters
    // build the query
    // return JSON
  } 

  ?>

My question is: How do I get all of the parameters out of $_POST['searchCriteria'] in the PHP script?

  • 写回答

2条回答 默认 最新

  • doutuo8800 2016-11-18 21:42
    关注

    If you want everything in a single $_POST parameter, you need to wrap in another object:

    $.post('api/summary.php', {searchCriteria : searchCriteria }, function(data) {
        ...
    });
    

    In the PHP, you would then access them as nested arrays.

    $sc = $_POST['searchCriteria']
    $rep = $sc['rep'];
    $num = $sc['num'];
    $uid = $sc['uid'];
    

    I'm not sure what you expect to gain by adding this extra level of wrapping.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计