2015-04-06 30 views
1

我使用下面的表格为什么数据不是从阿贾克斯传递到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值没有被传入,我不明白为什么,有没有人有任何指针?

+0

你看到任何JS错误控制台? – Diptendu 2015-04-06 09:17:31

+0

“if(){”?您应该使用javascript控制台(Chrome上的ctrl + maj + j) – 2015-04-06 09:17:43

+1

表单正在提交,并且您对数据键和POST变量使用不同的大小写? – adeneo 2015-04-06 09:20:28

回答

0

当您使用提交功能时,浏览器urlencode(正确)字段。在你的js代码中,你不是urlencoding。在大多数情况下,如果您在字段或任何其他需要编码的字符中输入空格,则会失败。 第二件事,在下面的行中,你必须为你的“描述”字段使用一个小写的“d”。 PHP区分大小写。 (我建议你总是使用小写字段名,以免出错)

尝试:

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

也许有更多的问题。就像Pierre Emmanuel所说的那样,使用javascript控制台,特别是“网络”选项卡来监视通过电线发送的内容。例如,您也可以看看PHP以var_dump($_POST);接收的内容。 (或var_dump(file_get_contents("php://input"));如果第一个命令失败。)

0

更改JavaScript代码的jQuery:

function editDescription(){ 
var name = $('#orginalName').val(); //getting input field value 
var description= $('#newDescription').val(); //getting input field value 
$.ajax({//create an ajax request to Your.php 
type: "POST", 
url: "Your.php", //your php file name 
data:{"name ":name,"description":description}, 
success: function(data) { 
    if (data) { 

     alert(data); 
    } 
    else { 
     alert('Successfully not posted.'); 
    } 
} 
}); 
} 
submitButton = document.getElementById("editDesButton"); 
submitButton.addEventListener("click", editDescription); 

Your.php

$Name = $_POST['name']; 
$Description = $_POST['description']; 

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