duanhuoyao7011 2015-04-03 10:51
浏览 34
已采纳

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

i am using the following form:

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

and using the following simple php, which when used with action=editProductDes.php works...

 $Name = $_POST['Name'];
 $Description = $_POST['newDescription'];

 if($Name !="" && $Description !=""){
 $sql = "UPDATE PRODUCTS SET P_Description = '$Description' WHERE P_NAME = '$Name'";
 $conn->exec($sql); 

and then when i use the following java script the data is not passed through and I cannot see why as I have a similar function and form where the JavaScript works fine, can anyone see why the data is not passing through?

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);
  • 写回答

1条回答 默认 最新

  • douzhan1963 2015-04-03 10:58
    关注

    You are posting to editDescription.PHP instead of editProductDes.php

    Change the following:

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

    You are also sending the data in your post under another name than you expect it to be in your PHP code (Description instead of newDescription) - change:

    xmlhttp.send("Name=" + name.value + "&newDescription=" + Description.value);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错