2016-11-26 93 views
1

我在我的網站的其他地方實施了相同的設置,但無法弄清楚爲什麼這次不工作。Php未收到來自JavaScript的信息

當用戶點擊接受按鈕時,它會調用一個JavaScript函數acceptOrder(orderID),它將orderID傳遞到php頁面以更新db中的記錄。

orderID在JavaScript中被分配好,但它沒有到達php。 POST上的Var_dump不顯示任何內容,$ _POST('orderID')也不顯示。我甚至嘗試發送一個整數到PHP的情況下,有問題的變種,但它沒有任何區別。

的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 
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); 
?> 

不用說,我已經搜查有關,並沒有看到其他地方的任何解決方案。

非常感謝

+0

你有沒有得到任何錯誤? –

+0

我已經發布了這個來自手機的內容,並刪除了部分圍繞微軟的js,所以忽略了這一點。 – Paul

+0

@HappyCoding沒有錯誤 – Paul

回答

0

我所看到的,你沒有爲單編號,代碼變線設置變量名

xmlhttp.send("orderID="+orderID); 

如果它唯一缺少的orderID的SQL錯誤,並所有其他passess好吧,這是你的解決方案。正如你在評論中所說的「我剛剛得到一個sql錯誤,因爲變量orderID是空的」。 你只是缺少命名發佈數據,這就是爲什麼它是空的。

+0

我認爲你是對的。這可能是我出錯的地方。當我回家時我會試試這個。非常感謝你 – Paul

+0

不客氣。 – Ultrazz008

+0

剛剛檢查,這工作!非常感謝你!! – Paul

0

請遵循這樣的

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
替換該行

xmlhttp=new ActiveXObject("Microsoft. } 

+0

是的,我知道,這些是通過手機複製/粘貼時的拼寫錯誤。他們在原始代碼中都可以。 – Paul

0

你應該在你的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"); 
} 
+0

是的,我知道,這些是通過手機複製/粘貼時的拼寫錯誤。他們在原始代碼中都可以。 – Paul

+0

什麼你進入控制檯 – Shibon

+0

@Shibon我剛剛得到一個SQL錯誤,因爲變量orderID爲空 – Paul

0

我會建議ÿ ou使用jQuery進行ajax調用。設置簡單並且簡單。特別是,因爲它對於初學者來說非常容易設置。對於想要以簡單的方式安裝ajax的人來說。我每次使用它我想在我的代碼中執行ajax。這裏是一個鏈接:

http://api.jquery.com/jquery.ajax/ 

只要把標籤包括jQuery,然後一個JavaScript命令來調用ajax。

xmlhttp.send(orderID); 

到: