2013-01-10 42 views
1

在我的地盤我有組合框在PHP中:如何發送參數POST方法在AJAX

<script type="text/javascript"> 
function showUserVisits(reservationObjectId) 
{ 
    //alert(reservationObjectId); 
    if (reservationObjectId == "") 
    { 
     document.getElementById("txtHint").innerHTML=""; 
     return; 
    } 
    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("txtHint").innerHTML=xmlhttp.responseText; 
     } 
    } 

    xmlhttp.open("GET","get_user.php?q=" +reservationObjectId,true); 
    xmlhttp.send(); 
}  
</script> 

<form> 
    <select name="users" onchange="showUserVisits(this.value)"> 
    <!-- <option value="">Select a person:</option> --> 
    <option value="1">aaa1</option> 
    <option value="2">aaa2</option> 
    <option value="3">aaa3</option> 
    <option value="4">aaa4</option> 
    </select> 
    </form> 

當組合框的用戶變化的項目,方法showUserVisits由AJAX調用。我必須將reservationObjectId傳遞給get_user.php站點。如何完成GET方法,我想通過POST方法傳遞此參數,因爲在GET方法中,有人可以更改ID。我該怎麼做 ?

感謝

+0

不要被愚弄,也可以編輯郵件項目,就像獲取項目。只要確保你總是檢查你的數據。 –

+0

@John你的意思是,如果請求通過'post'方法發送參數將顯示在url中? – tnanoba

+0

@DaHaKa:不在URL中,但可以使用瀏覽器擴展輕鬆修改POST參數 – xbonez

回答

2

改變這一行:

xmlhttp.open("GET","get_user.php?q=" +reservationObjectId,true); 

要這樣:

xmlhttp.open("POST", url,true); 

這裏看到更多的信息:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms757849(v=vs.85).aspx

下面是一些例子:

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

其中一個例子(從上面的鏈接):

var url = "get_data.php"; 
var params = "lorem=ipsum&name=binny"; 
http.open("POST", url, true); 

//Send the proper header information along with the request 
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
http.setRequestHeader("Content-length", params.length); 
http.setRequestHeader("Connection", "close"); 

http.onreadystatechange = function() {//Call a function when the state changes. 
    if(http.readyState == 4 && http.status == 200) { 
     alert(http.responseText); 
    } 
} 
http.send(params); 
+1

以這種方式發送POST值嗎?它看起來像是作爲GET值發送的,但是s也是通過POST這種方式發送的? –

+0

+1謝謝指出!更正的答案 – pzirkind

2

您添加鍵值-VAL對在.send(),像這樣:

xmlhttp.open("POST","get_user.php",true); 
xmlhttp.send("reservationId=" + reservationObjectId); 

要添加多個鍵值對,使用&作爲分隔符。

在您的服務器端腳本中,您將可以在$_POST['reservationID']中訪問它。

1
xmlhttp.open("POST", 'get_user.php', true); 
xmlhttp.send("q="+reservationObjectId); 

但是要記住,POST數據也可以被編輯,所以在你的PHP文件,檢查值!

$q = (int) $_POST['q']; 

,並確保預定ID屬於用戶

+0

對不起,但這不起作用,它什麼都不發送,我不得不添加http.setRequestHeader(「Content-type」,「application/x-www-form-urlencoded」); http.setRequestHeader(「Content-length」,params.length); http.setRequestHeader(「Content-length」,params.length); http.setRequestHeader(「Connection」,「close」);現在參數被髮送 – Robert

+0

@Robert你只需要設置內容類型。 – Musa

0

下面的代碼演示瞭如何做一個職位,但與POST方法別人也可以更改ID

xmlhttp.open("POST","get_user.php",true); 
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
xmlhttp.send("q="+encodeURIComponent(reservationObjectId)); 
0
<script type="text/javascript"> 
function showUserVisits(reservationObjectId) 
{ 
//alert(reservationObjectId); 
if (reservationObjectId == "") 
{ 
    document.getElementById("txtHint").innerHTML=""; 
    return; 
} 
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("txtHint").innerHTML=xmlhttp.responseText; 
    } 
} 
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");   // set header in case of post method 
xmlhttp.open("POST","get_user.php",true); // set post method 
xmlhttp.send("q="+reservationObjectId); // set data 

}

相關問題