Javascript http_build_query
Generates a form-encoded query string from an associative array or object.
Example 1
Running
1.http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&');
Could return
1.'foo=bar&php=hypertext+processor&baz=boom&cow=milk'
Example 2
Running
1.http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_');
Could return
1.'php=hypertext+processor&myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk'
function http_build_query( formdata, numeric_prefix, arg_separator ) {
// Generates a form-encoded query string from an associative array or object.
//
// version: 810.114
// discuss at: http://phpjs.org/functions/http_build_query
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Legaev Andrey
// + improved by: Michael White (http://getsprink.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: urlencode
// * example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&');
// * returns 1: 'foo=bar&php=hypertext+processor&baz=boom&cow=milk'
// * example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_');
// * returns 2: 'php=hypertext+processor&myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk'
var key, use_val, use_key, i = 0, j=0, tmp_arr = [];
if (!arg_separator) {
arg_separator = '&';
}
for (key in formdata) {
use_val = urlencode(formdata[key].toString());
use_key = urlencode(key);
if (numeric_prefix && !isNaN(key)) {
use_key = numeric_prefix + j;
j++;
}
tmp_arr[i++] = use_key + '=' + use_val;
}
return tmp_arr.join(arg_separator);
}
|
HTML code for linking to this page:
Related in same category :
-
-
-
-
-
-
-
-
-
|
|