2013-01-10 41 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 

}

相关问题