我使用以下表格:
<form id="dataForm" method="post">
<h2 id="formheader"> Update Product 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="newDescription">
</div>
<div id="theSubmit">
<button id="editDescription">Submit</button>
</div>
</form>
并使用以下简单的php,当与action=editProductDes.php
一起使用时...
$Name = $_POST['Name'];
$Description = $_POST['newDescription'];
if($Name !="" && $Description !=""){
$sql = "UPDATE PRODUCTS SET P_Description = '$Description' WHERE P_NAME = '$Name'";
$conn->exec($sql);
然后当我使用下面的java脚本时,数据没有通过,我无法理解为什么因为我有一个类似的函数和表单,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("editDescription");
submitButton.addEventListener("click", editDescription);
答案 0 :(得分:0)
您要发布到editDescription.PHP
而不是editProductDes.php
更改以下内容:
xmlhttp.open("POST","editProductDes.php",true);
您还发布了帖子中的数据,而不是您希望它在PHP代码中的名称(Description
而不是newDescription
) - 更改:
xmlhttp.send("Name=" + name.value + "&newDescription=" + Description.value);