<?php
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
/* Output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
*/
How to do similar thing in javascript,say, get the query string from the array ,and convert the array to query string?
UPDATE
the jquery plugin is not working:
var fromVar = $.query.load('?cow=milk')
fromVar.set('first', 'value');
fromVar.toString()
It outputs ?cow=milk
while I want it to be ?cow=milk&first=value
Nowadays, you can simply use the URLSearchParams
constructor on your object.
const data = {
"test1": 1,
"test2": "two",
"test3": 3,
};
const params = new URLSearchParams(data);
console.log(params.toString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With