[英]Php not receiving post from JavaScript
I have implemented the same setup elsewhere in my site and can't figure out why it's not working this time. 我已经在我的网站的其他地方实现了相同的设置,并且无法弄清楚为什么它这次没有工作。
When a user clicks the accept button, it calls a JavaScript function acceptOrder(orderID) which passes the orderID onto a php page to update a record in the db. 当用户单击“接受”按钮时,它会调用JavaScript函数acceptOrder(orderID),该函数将orderID传递到php页面以更新db中的记录。
orderID is assigned ok in the JavaScript but it doesn't reach the php. orderID在JavaScript中分配好,但它没有到达php。 Var_dump on POST shows nothing, nor does $_POST('orderID').
POST上的Var_dump什么也没有显示,$ _POST('orderID')也没有。 I've even tried just sending an integer to the php in case there was a problem with the var but it made no difference.
我甚至尝试过向php发送一个整数,以防var出现问题,但没有任何区别。
Js JS
function acceptOrder(orderID) {
var orderID=orderID;
console.log("assigned: "+orderID);
var xmlhttp;
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
xmlhttp=new ActiveXObject("Microsoft. }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
console.log (xmlhttp.responseText);
}
}
xmlhttp.open("POST","acceptorder.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-rule encoded");
xmlhttp.send(orderID);
console.log(orderID+" sent");
//location.reload();
//console.log("reload");
}
Php 腓
<?php
require_once("config1412/class.shop.php");
session_start();
$shop = new SHOP();
echo var_dump($_POST);
//$orderID = $_POST['orderID'];
//echo "orderId var = ".$orderID."<br/>post ".$_POST['orderID'];
//$shop->acceptOrder($orderID);
?>
Needless to say I've searched about and don't see any solutions elsewhere. 毋庸置疑,我已经搜索过,并没有在其他地方看到任何解决方案。
Many thanks 非常感谢
Please replace this line 请更换此行
xmlhttp=new ActiveXObject("Microsoft. }
with following this line 遵循这一行
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
You should write this in your js 你应该在你的js中写这个
function acceptOrder(orderID) {
var orderID=orderID;
console.log("assigned: "+orderID);
var xmlhttp;
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE6, IE5
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
console.log (xmlhttp.responseText);
}
}
xmlhttp.open("POST","acceptorder.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-rule encoded");
xmlhttp.send(orderID);
console.log(orderID+" sent");
//location.reload();
//console.log("reload");
}
I would recommend you to use jQuery for ajax calls. 我建议你使用jQuery进行ajax调用。 It's much easier to setup and straightforward.
它更容易设置和简单。 Especially, and specialy because it is very easy setup for a beginner.
特别是,特别是因为它非常容易为初学者设置。 And for people who want to install ajax the easy way.
对于想要安装ajax的人来说,这是一种简单的方法。 I use it every single time I want to do ajax in my code.
我每次都想在代码中使用ajax时使用它。 Here is a link:
这是一个链接:
http://api.jquery.com/jquery.ajax/
Just put the tag to include jQuery and then one javascript command to call the ajax. 只需将标记包含在jQuery中,然后使用一个javascript命令调用ajax即可。
As i can see, you didn't set variable name for orderID, change line of code: 我可以看到,你没有为orderID设置变量名,更改代码行:
xmlhttp.send(orderID);
to: 至:
xmlhttp.send("orderID="+orderID);
If it's only SQL error of missing orderID, and all other passess okay, it's solution for you. 如果它只是丢失orderID的SQL错误,并且所有其他passess没问题,那么它就是你的解决方案。 As you said in comments "I just get a sql error because the variable orderID is empty".
正如你在评论中所说的“我只是因为变量orderID为空而得到sql错误”。 You're missing only naming post send data, that's why it's empty.
你只缺少命名发送数据,这就是为什么它是空的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.