在我的网站上,我在php中使用了combobox:
<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 :(得分:2)
您在.send()
中添加了键值对,如下所示:
xmlhttp.open("POST","get_user.php",true);
xmlhttp.send("reservationId=" + reservationObjectId);
要添加多个键值对,请使用&
作为分隔符。
在您的服务器端脚本中,您将能够在$_POST['reservationID']
中访问它。
答案 1 :(得分: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);
答案 2 :(得分:1)
xmlhttp.open( "POST", 'get_user.php', true );
xmlhttp.send( "q="+reservationObjectId );
但请记住,帖子数据也可以编辑,所以在你的php文件中,检查值!
$q = (int) $_POST['q'];
确保reservationID属于用户
答案 3 :(得分:0)
下面的代码显示了如何发布帖子,但是使用POST方法,有人也可以更改id
xmlhttp.open("POST","get_user.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q="+encodeURIComponent(reservationObjectId));
答案 4 :(得分: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
}