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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么