2011-11-07 30 views
0

所以我必須重新尋找一些非常基本的東西。我試圖發送表單數據來更新使用ajax http post請求的mysql記錄。我有一個表單的頁面。在窗體上,我有一個提交按鈕,從一個單獨的js文件調用http請求,該js文件調用一個php文件。使用螢火蟲,它看起來不像我有任何錯誤,但當我「打印」請求返回它的SQL不傳遞實際變量,它只是傳遞「$ _POST ['name']」literallty。HttpRequest xhr使用表格vaiables發佈

它ruturns的SQL:

UPDATE contacts SET name= "$_POST['name']" , phone = "$_POST['phone']" WHERE id = "$_POST['id']" 

而不是傳遞中的實際變量值。我的問題是我如何通過實際varialbe數據,使其返回somethign像:

UPDATE contacts SET name= "Mike", phone = "303-333-3333" WHERE id = "001" 

我的形式(不包含它周圍的表單標籤)是這樣的:

<label> 
     <input type="text" name="name" id="name" /> 
    </label> 
    <label> 
     <input type="text" name="phone" id="phone" /> 
    </label> 
    <label> 
     <input type="hidden" name="id" id="id" /> 
    </label> 
    <label> 
     <input onclick="sendData()"type="submit" name="button" id="button" value="Submit" /> 
    </label> 

我在一個單獨的文件JS的樣子:

function sendData() 
{ 

if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("center").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("POST","xhr_php/send.php",true); 
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
xmlhttp.send("name={$_POST['name']}&phone={$_POST['phone']}&id={$_POST['id']} "); 
} 

我send.php文件看起來像:

db_connection include 

$name= $_POST['name']; 
$phone= $_POST['phone']; 
$id = $_POST['id']; 

print $query = "UPDATE contacts SET 
     name = '{$name}', 
     phone = '{$phone}', 
WHERE id= {$id}"; 

$results= mysql_query($query, $db_connection); 
if(mysql_affected_rows()==1){ 
    echo "Success"; 
} 
if(mysql_affected_rows()==0){ 
    echo "failed"; 
} 

再次,一切似乎在調用正確的文件方面正常工作,它只是沒有傳遞任何可變數據。任何幫助將非常感激。謝謝。

回答

0

我相信錯誤是上線

xmlhttp.send("name={$_POST['name']}&phone={$_POST['phone']}&id={$_POST['id']} "); 

改變它的東西是這樣的:

var name = document.getElementById('name').value; 
var phone = document.getElementById('phone').value; 
var id = document.getElementById('id').value; 
xmlhttp.send("name="+name+"&phone="+phone+"&id="+id); 
+0

謝謝您的幫助。這就是訣竅,我知道這很簡單。謝謝! – user982853