santosh vishwakarma
santosh vishwakarma

Reputation: 13

sending array from javascript through url in php page but

Rooms are an array

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

sending from javascript to php page on php page url show full array value which are store in array in page address bar url like that

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

I want to prevent this data which show in url %22id%22:10,%22rate%22:100

I am decoding on php page any other way to send array data from javascript to php page

Upvotes: 1

Views: 2646

Answers (4)

Scalpweb
Scalpweb

Reputation: 1971

The only way to send data to another page without showing them in the url is to use POST.

Basically, you can put your data into an invisible form input :

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

Upvotes: 3

christopher
christopher

Reputation: 27346

Why not just POST the data instead then?

For example, with jQuery:

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

The advantage is you're not passing some horrific URL. As an added bonus, this example is also asynchronous, so the user doesn't see any refresh in their browser.

Non-Framework Version

If you don't wish to use jQuery, you can do this with pure Javascript, using the XMLHttpRequest object, like so:

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

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

You can use a POST request, however this would require generating and submitting a form:

// 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();

Upvotes: 0

Christoph Diegelmann
Christoph Diegelmann

Reputation: 2034

You can use a hidden form and the post method. Then you would use $_POST instead of $_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>

Upvotes: 1

Related Questions