3
data.push({
    name: name,
    age: age,
    address: address,
    contactnumber: contactnumber

});

var url = '../reports/student.php?data='+data;
window.open(url, '_blank');

../reports/student.php?data=[object%20Object]

Above is an object i want to send to another page so i can retrieve it and use it on the next page to generate a report. The url is looking like the one above i am not sure if this is possible and if possible how can i get the value of each data in the object.If not possible please point me to the right direction any idea is ap

Pekka
  • 1,075
  • 2
  • 10
  • 17

2 Answers2

8

if you definetly want to use GET request, use JSON.stringify:

var url = '../reports/student.php?data='+ encodeURIComponent(JSON.stringify(data));

But better way will be a POST request.

You also can use jQuery.param():

var data = {
  name: 'yourname',
  age: 1,
}

var params = jQuery.param(data) // 'name=yourname&age=1'
var url = '../reports/student.php?'+ params;
marsh
  • 1,431
  • 1
  • 13
  • 19
  • it is ok if i dont stringify the data before sending i mean i want to do all retrieving in the next page?i mean i dont want to put stingify in the sending of data in url is it possible and how can i get the data now that i have sent it using jquery not php – Pekka Apr 29 '15 at 08:20
  • my question is how will i get the value now in second page? – Pekka Apr 29 '15 at 08:26
  • there is simple function for parsing `get` params by JS http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – marsh Apr 29 '15 at 08:29
  • should i pass the entire url to the function of just the `data`? – Pekka Apr 29 '15 at 08:32
  • for get a `data` param you call like this: `getParameterByName('data')` – marsh Apr 29 '15 at 08:35
1

jQuery.param():

Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request. In case a jQuery object is passed, it should contain input elements with name/value properties.

The doc also states, that

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray()

So in your case:

var url = '../reports/student.php?' + jQuery.param(serializeArray(data));

To unserialize you should use jQuery BBQ's deparam function, as per cce's answer in this SO question: The $.param( ) inverse function in JavaScript / jQuery.

Community
  • 1
  • 1
Taz
  • 3,718
  • 2
  • 37
  • 59