我使用下面的表格爲什麼數據不是從阿賈克斯傳遞到PHP
<form id="dataForm" method="post">
<h2 id="formheader"> Update Description</h2>
<div>
<label>Product Name:</label>
<input class="inputForm" id="orginalName" type="text" name="Name">
</div>
<div>
<label>New Description:</label>
<input class="inputForm" id="newDescription" type="text" name="description">
</div>
<div id="theSubmit">
<button id="editDesButton">Submit</button>
</div>
</form>
</section>
和javascript函數
function editDescription(){
xmlhttp = new XMLHttpRequest();
var name = document.getElementById("orginalName");
var Description = document.getElementById("newDescription");
var data_seen = false;
// this is a flag to record whether any data has been seen. Used in the guard ofthe alert statement.
if (name.value !="" && Description.value !=""){
data_seen = true;
xmlhttp.open("POST","editDescription.PHP",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("Name=" + name.value + "&Description=" + Description.value);
}
if (!data_seen) {
alert("please enter some data");
}
}
submitButton = document.getElementById("editDesButton");
submitButton.addEventListener("click", editDescription);
和PHP的這個小位以下
$Name = $_POST['Name'];
$Description = $_POST['description'];
if($Name !="" && $Description !=""){
$sql = "UPDATE PRODUCTS SET P_Description = '$Description' WHERE P_NAME = '$Name'";
$conn->exec($sql);
如果我運行表格並使用action="editDescription.php
,那麼sql會運行並且表格會更新爲我想要的樣式,但是噹噹按鈕被點擊時,我在事件上運行javascript值沒有被傳入,我不明白爲什麼,有沒有人有任何指針?
你看到任何JS錯誤控制檯? – Diptendu 2015-04-06 09:17:31
「if(){」?您應該使用javascript控制檯(Chrome上的ctrl + maj + j) – 2015-04-06 09:17:43
表單正在提交,並且您對數據鍵和POST變量使用不同的大小寫? – adeneo 2015-04-06 09:20:28