jQueryPosttoPHP文件可以工作,但只能在php中回显时返回一些数据
//用AJAX发送数据的Jquery代码jQueryPosttoPHP文件可以工作,但只能在php中回显时返回一些数据,php,jquery,post,Php,Jquery,Post,//用AJAX发送数据的Jquery代码 $.ajax({ type: "POST", url: "test.php", data: "fname="+ fname + "& lname="+ lname + "& address="+ address + "& ci
$.ajax({
type: "POST",
url: "test.php",
data:
"fname="+ fname +
"& lname="+ lname +
"& address="+ address +
"& city="+ city +
"& state="+ state +
"& zip="+ zip +
"& phone="+ phone +
"& useremail="+ useremail +
//the following values are not being receieved by the php correctly
"& subtotal="+ subTotal +
"& quantity="+ quantity,
success: function(){
$('#oderBtn').hide(function({$('#orderTest').fadeOut();});
}
});
//接收AJAX数据的PHP代码
$fname = htmlspecialchars(trim($_POST['fname']));
$lname = htmlspecialchars(trim($_POST['lname']));
$city = htmlspecialchars(trim($_POST['city']));
$state = htmlspecialchars(trim($_POST['state']));
$zip = htmlspecialchars(trim($_POST['zip']));
$address = htmlspecialchars(trim($_POST['address']));
$email = htmlspecialchars(trim($_POST['useremail']));
//these do not post correctly, i do not know why
$subTotal = htmlspecialchars(trim($_POST['subtotal']));
$quantity = htmlspecialchars(trim($_POST['quantity']));
所以问题是fname、lname、city、state、zip、address和email都可以正常工作,但小计和数量都不能正常工作,firebug让它们以相同的方式发布,看起来PHP只是没有正确接收数据
正在添加回显文件\u获取\u内容(“php://input"); 到php会得到所有发送回显的内容,包括小计和数量,但是仅仅做$u POST['subtotal']不会得到值
谢谢您在这件事上的帮助。您是否尝试过使用
$\u GET[]
而不是$\u POST[]
?这将是我要尝试的第一件事,因为$\u GET
设计用于从URL获取数据,而$\u POST
设计用于从表单提交中获取传递的数据
$fname = htmlspecialchars(trim($_GET['fname']));
$lname = htmlspecialchars(trim($_GET['lname']));
$city = htmlspecialchars(trim($_GET['city']));
$state = htmlspecialchars(trim($_GET['state']));
$zip = htmlspecialchars(trim($_GET['zip']));
$address = htmlspecialchars(trim($_GET['address']));
$email = htmlspecialchars(trim($_GET['useremail']));
$subTotal = htmlspecialchars(trim($_GET['subtotal']));
$quantity = htmlspecialchars(trim($_GET['quantity']));
此输出作为JSON数组的示例:
$data = array(
'fname' => htmlspecialchars(trim($_GET['fname']));
'lname' => htmlspecialchars(trim($_GET['lname']));
'city' => htmlspecialchars(trim($_GET['city']));
'state' => htmlspecialchars(trim($_GET['state']));
'zip' => htmlspecialchars(trim($_GET['zip']));
'address' => htmlspecialchars(trim($_GET['address']));
'email' => htmlspecialchars(trim($_GET['useremail']));
'subTotal' => htmlspecialchars(trim($_GET['subtotal']));
'quantity' => htmlspecialchars(trim($_GET['quantity']));
);
echo json_encode($data);
编辑:
您可能还需要在jQuery中使用encodeURI()
,以确保没有符号或其他字符弄乱URL字符串
$.ajax({
type: "GET",
url: "test.php",
data: encodeURI(
"?fname=" + fname +
"&lname=" + lname +
"&address=" + address +
"&city=" + city +
"&state=" + state +
"&zip=" + zip +
"&phone=" + phone +
"&useremail=" + useremail +
"&subtotal=" + subTotal +
"&quantity=" + quantity
),
success: function(response){
alert(response); // Will show the JSON array
$('#oderBtn').hide(function({$('#orderTest').fadeOut();});
}
});
小提琴:首先,您不需要构建这样的字符串。您只需传递一个对象文本:
$.ajax({
type: "POST",
url: "test.php",
data: {
fname: fname,
lname: lname
...
},
success: function(){
$('#oderBtn').hide(function({$('#orderTest').fadeOut();});
}
});
jQuery将根据需要对其进行序列化。我认为这是令人印象深刻的,因为您正在传递一个查询字符串,所以它完全满足了您的POST请求
而且,它在收到电子邮件后停止工作,这看起来很可疑。你能粘贴
print\r($\u POST)的结果吗吗?这似乎是一种奇怪的数据格式化方式。您是否尝试过这样格式化数据:数据:{fname:fname,lname:lname,address:address,…}
?数组([fname]=>[amp;\u lname]=>[amp;\u地址]=>[amp;\u城市]=>[amp;\u州]=>AL[amp;\u zip]=>[amp;\u电话]=>[amp;\u用户电子邮件]=>[amp;\u小计]=>5[amp;\u数量]=>1)以下是print_r($_POST)的回复;请记住,除了所讨论的值之外,我没有在表单中填写任何数据。