我想通过AJAX发送一组变量到PHP脚本。JQUERY AJAX发送搜索条件到PHP返回JSON
通常情况下,这是我会怎么做:
$('.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);
});
});
这就是我通常会做到这一点。
但现在,我想发送的所有参数的一个变量我打电话searchCriteria,如下:
$('.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);
});
});
然后,在PHP脚本,从变量检索所有参数处理:
<?php
if($_POST['searchCriteria'] == true)
{
// get the parameters
// build the query
// return JSON
}
?>
我的问题是:如何获得的所有参数进行$ _ POST [“searchCriteria”]在PHP脚本?
如果你这样发送,你不会在PHP端获得一个searchCriteria参数,而是'rep'and'num'和'uid'。 – Quagaar
只需将它包装在一个更大的对象{searchCriteria:searchCriteria}中,然后您将使用$ _POST ['searchCriteria']检索它 – Carlos2W
如果您以@ Carlos2W的身份执行操作,请使用'json_decode'将JSON数据转换为PHP数组或对象。 – Quagaar