1

在我的网站上,我在 php 中有组合框:

<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>

当用户更改组合框中的项目时,ajax 会调用方法 showUserVisits。而且我必须将 reservationObjectId 传递给 get_user.php 站点。它是如何完成的 GET 方法,我想通过 POST 方法传递这个参数,因为在 GET 方法中有人可以更改 id。我该怎么做 ?

谢谢

4

5 回答 5

2

您在 中添加 key-val 对.send(),如下所示:

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

要添加多个键值对,&请用作分隔符。

在您的服务器端脚本中,您将能够在$_POST['reservationID'].

于 2013-01-10T20:41:16.603 回答
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);
于 2013-01-10T20:42:18.623 回答
1
xmlhttp.open( "POST", 'get_user.php', true );
xmlhttp.send( "q="+reservationObjectId );  

但请记住,帖子数据也可以编辑,所以在你的 php 文件中,检查值!

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

并确保 reservationID 属于用户

于 2013-01-10T20:42:59.307 回答
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

}

于 2013-06-25T06:28:35.010 回答
0

下面的代码显示了如何发帖,但是使用 POST 方法也可以更改 id

xmlhttp.open("POST","get_user.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q="+encodeURIComponent(reservationObjectId));
于 2013-01-10T20:43:11.250 回答