1

房间是一个数组

window.location = "booking_status.php?array="+ JSON.stringify(rooms);

从javascript发送到php页面url上的php页面显示完整的数组值,这些值存储在页面地址栏url中的数组中

http://localhost/zalawadi/booking_status.php?array=[ {%22id%22:10,%22rate%22:100} ]

我想阻止显示在 url 中的这些数据%22id%22:10,%22rate%22:100

我在 php 页面上解码以任何其他方式将数组数据从 javascript 发送到 php页面

4

4 回答 4

3

将数据发送到另一个页面而不在 url 中显示它们的唯一方法是使用 POST。

基本上,您可以将数据放入不可见的表单输入中:

<form method="post" id="form" action="booking_status.php">
    <input name="array" id="array" type="hidden" value="" />
</form>
<a href="" onclick="sendForm(); return false;">Send</a>
<script type="text/javascript">
    function sendForm(){
        document.getElementById('array').value = JSON.stringify(rooms);
        document.getElementById('form').submit(); //fixed syntax
    }
</script>
于 2013-09-03T15:00:39.660 回答
1

您可以使用隐藏表单和 post 方法。然后您将使用 $_POST 而不是 $_GET。

<form action="script.php" onsubmit="this.firstChild.value=JSON.stringify(value);">
    <input type="hidden" value="" />
    <a href="javascript:this.form.submit();">Link text</a>
</form>
于 2013-09-03T14:59:12.123 回答
0

您可以使用 POST 请求,但这需要生成并提交表单:

// assuming `rooms` already defined
var frm = document.createElement('form'), inp = document.createElement('input');
frm.action = "booking_status.php";
frm.method = "post";
inp.type = "hidden";
inp.name = "array";
inp.value = JSON.stringify(rooms);
frm.appendChild(inp);
document.body.appendChild(frm);
frm.submit();
于 2013-09-03T15:00:33.727 回答
0

为什么不只是POST数据呢?

例如,使用jQuery

$.ajax({
  type: "POST",
  url: "booking_status.php",
  data: JSON.stringify(rooms),
  success: // add success function here!
});

优点是您没有传递一些可怕的 URL。作为一个额外的好处,这个例子也是异步的,所以用户在他们的浏览器中看不到任何刷新。

非框架版本

如果您不想使用jQuery,您可以使用纯 Javascript 来执行此操作,使用XMLHttpRequest对象,如下所示:

var url = "get_data.php";
var param = JSON.stringify(rooms);

var http = new XMLHttpRequest();
http.open("POST", url, true);


http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
    // Request has gone well. Add something here.
    }
}
http.send(param);
于 2013-09-03T15:00:16.687 回答