为什么数据没有通过javascript传递到php


why is the data not getting passed through the javascript to the php

我使用以下表单:

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

您要过帐到editDescription.PHP而不是editProductDes.php

更改以下内容:

xmlhttp.open("POST","editProductDes.php",true);

你还在你的帖子中用另一个名字发送数据,而不是你期望的PHP代码中的数据(Description而不是newDescription)-更改:

xmlhttp.send("Name=" + name.value + "&newDescription=" + Description.value);