[英]AJAX POST group of form variables to PHP
I am trying to send a group of form parameters over to a PHP script for processing. 我正在尝试将一组表单参数发送到PHP脚本进行处理。
I've previously done something like this using $.post
, but now I'm trying to get it done strictly by using $.ajax
. 我以前曾使用
$.post
做过这样的事情,但现在我试图通过使用$.ajax
来严格完成它。
Here is the jQuery click event that is supposed to send all of the variables to the PHP script: 这是应该将所有变量发送到PHP脚本的jQuery click事件:
$('.searchSubmit').on('click', function()
{
var searchCriteria = {
import_bill: $('#import_bill').val(),
import_ramp: $('#import_ramp').val(),
import_delivery: $('#import_delivery').val(),
// few more form parameters
};
$.ajax({
url: 'api/railmbs.php', // process script
type: 'POST',
data: searchCriteria, // parameter group above
dataType: 'html' // had this set to json, but only got fail
success: function(data, textStatus, jqXHR)
{
console.log(data);
},
error: function(jqHHR, textStatus, errorThrown)
{
console.log('fail');
}
});
});
Here is the PHP script, called railmbs.php: 这是一个名为railmbs.php的PHP脚本:
<?php
if(isset($_POST['searchCriteria']))
{
$value = $_POST['searchCriteria'];
$_SESSION['where'] = "";
$import_bill = mysqli_real_escape_string($dbc, trim($value['import_bill']));
$import_ramp = mysqli_real_escape_string($dbc, trim($value['import_ramp']));
$import_delivery = mysqli_real_escape_string($dbc, trim($value['import_delivery']));
echo $import_bill; // just trying to echo anything at this point
}
?>
Not sure what I am doing wrong. 不知道我在做什么错。 If I
echo
hello
before the IF
above, the console will output accordingly. 如果我
echo
hello
的前IF
以上,因此控制台将输出。 But I cannot seem to get anything to echo
from inside the IF
. 但是我似乎无法从
IF
内部得到任何echo
。
Does anyone see my error? 有人看到我的错误吗?
First of all. 首先。 Why not to use
$("form").serialize()
? 为什么不使用
$("form").serialize()
? It would be much cleaner. 这样会更清洁。 Secondary, you transfer data in root object, so to get you values, check
$_POST
array. 第二,在根对象中传输数据,因此要获取值,请检查
$_POST
数组。 Instead of $value = $_POST['searchCriteria']
use $value = $_POST;
代替
$value = $_POST['searchCriteria']
使用$value = $_POST;
. 。 This PHP code should work:
此PHP代码应该可以工作:
<?php
if(isset($_POST))
{
$_SESSION['where'] = "";
$import_bill = mysqli_real_escape_string($dbc, trim($_POST['import_bill']));
$import_ramp = mysqli_real_escape_string($dbc, trim($_POST['import_ramp']));
$import_delivery = mysqli_real_escape_string($dbc, trim($_POST['import_delivery']));
echo $import_bill; // just trying to echo anything at this point
}
?>
Or modify your js to send data in searchCriteria
object, like this: 或修改您的js以在
searchCriteria
对象中发送数据,如下所示:
var searchCriteria = {
searchCriteria: {
import_bill: $('#import_bill').val(),
import_ramp: $('#import_ramp').val(),
import_delivery: $('#import_delivery').val(),
// few more form parameters
}};
You are not setting the "searchCriteria" variable. 您没有设置“ searchCriteria”变量。
Change this: 更改此:
$('.searchSubmit').on('click', function()
{
var searchCriteria = {
import_bill: $('#import_bill').val(),
import_ramp: $('#import_ramp').val(),
import_delivery: $('#import_delivery').val(),
// few more form parameters
};
$.ajax({
url: 'api/railmbs.php', // process script
type: 'POST',
data: searchCriteria, // parameter group above
dataType: 'html' // had this set to json, but only got fail
success: function(data, textStatus, jqXHR)
{
console.log(data);
},
error: function(jqHHR, textStatus, errorThrown)
{
console.log('fail');
}
});
});
to: 至:
$('.searchSubmit').on('click', function()
{
var data = {
searchCriteria: {
import_bill: $('#import_bill').val(),
import_ramp: $('#import_ramp').val(),
import_delivery: $('#import_delivery').val(),
// few more form parameters
}
};
$.ajax({
url: 'api/railmbs.php', // process script
type: 'POST',
data: data, // parameter group above
dataType: 'html' // had this set to json, but only got fail
success: function(data, textStatus, jqXHR)
{
console.log(data);
},
error: function(jqHHR, textStatus, errorThrown)
{
console.log('fail');
}
});
You should check if you actually send post data using your browser developer tools or typing var_dump($_POST);
您应该检查是否使用浏览器开发人员工具或键入
var_dump($_POST);
实际发送了帖子数据var_dump($_POST);
at the beginning of your PHP script. 在PHP脚本的开头。
As far as i can see, you never actually set searchCriteria
as post variable. 据我所知,您实际上从未将
searchCriteria
设置为post变量。
Currently your $_POST
variable should contain the field import_bill
, import_ramp
and so on. 当前,您的
$_POST
变量应包含字段import_bill
, import_ramp
等。 Either change your if
statement or your JavaScript object to {searchCriteria: {/*Your data here*/}
. 将您的
if
语句或JavaScript对象更改为{searchCriteria: {/*Your data here*/}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.