phpjs
Version:
378 lines (297 loc) • 10.4 kB
HTML
<!-- Generated by Rakefile:build -->
<strong>
<a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
</strong>
on 2012-06-16 08:17:54 <br />
@Click Button Publishing: Glad to hear it is working for you! (I'm trying to help catch up on old messages one-by-one.)
<hr />
<strong>
<a href="www.olivagreca.com" rel="nofollow">Tom </a>
</strong>
on 2012-05-12 11:04:41 <br />
Hello guys,
I am trying to produce a query string like:
//localhost:8080/select/?q=patata&model:journal+model:member+model:new_member+model:book
But when i am using the below code block i have an output like:
//localhost:8080/select/?q=patata&model:journal
How can i use repetitive pair values?
Thank you in advance
<pre><code>
<script type="text/javascript">
var a = "member";
var b = "new_member";
var c = "book";
var d = "journal";
var e = "cds";
var f = "pdfs";
function http_build_query (formdata, numeric_prefix, arg_separator) {
var value, key, tmp = [],
that = this;
var _http_build_query_helper = function (key, val, arg_separator) {
var k, tmp = [];
if (val === true) {
val = "1";
} else if (val === false) {
val = "0";
}
if (val != null) {
if(typeof(val) === "object") {
for (k in val) {
if (val[k] != null) {
tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));
}
}
return tmp.join(arg_separator);
} else if (typeof(val) !== "function") {
return that.urlencode(key) + ":" + that.urlencode(val);
} else {
throw new Error('There was an error processing for http_build_query().');
}
} else {
return '';
}
};
if (!arg_separator) {
arg_separator = "&";
}
for (key in formdata) {
value = formdata[key];
if (numeric_prefix && !isNaN(key)) {
key = String(numeric_prefix) + key;
}
var query=_http_build_query_helper(key, value, arg_separator);
if(query != '') {
tmp.push(query);
}
}
return tmp.join(arg_separator);
}
function urlencode (str) {
str = (str + '').toString();
// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
var aa = http_build_query({'model': a, 'model': b, 'model': c, 'model': d}, '', '+');
var value= "patata&";
var qurl = "//localhost:8080/select/?q=" + value + aa
console.log(qurl);
</script>
</code></pre>
<hr />
<strong>
<a href="140plus.2et.in" rel="nofollow">Click Button Publishing</a>
</strong>
on 2011-11-07 12:43:14 <br />
We are using this code to format our URL and it is superb. Thanks phpJS :)
<hr />
<strong>
Dreamer
</strong>
on 2010-10-23 11:41:22 <br />
@Brett Zamir: It works now.
<hr />
<strong>
<a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
</strong>
on 2010-10-22 22:04:04 <br />
@Dreamer: Thanks for the report; should be fixed in Git: http://github.com/kvz/phpjs/raw/master/functions/url/http_build_query.js . Let us know if that works...
<hr />
<strong>
Dreamer
</strong>
on 2010-10-22 10:33:11 <br />
Bug report: this.urlencode is not a function
Related to php.default.namespaced.min.js version 3.19
<pre><code>
var $P = new PHP_JS();
$P.http_build_query({foo: 'bar'});
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2009-04-14 12:02:53 <br />
Alright! Nice! Saved it in SVN and will be online & packaged shortly
<hr />
<strong>
stag019
</strong>
on 2009-04-05 20:20:16 <br />
D'oh.
Change line 2 from "var key, tmp = []," to "var value, key, tmp = [],".
Add "value = formdata[key];" under line 26.
Change line 30 (or new line 31 if you've added the previous line from "tmp.push(_http_build_query_helper(key, formdata[key], arg_separator));" to "tmp.push(_http_build_query_helper(key, value, arg_separator));".
That should fix'er.
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2009-04-03 13:22:28 <br />
@ stag019: Nice! But I can only replace the current with your implementation if it doesn't break the second example (it does now).
<hr />
<strong>
stag019
</strong>
on 2009-03-30 03:43:46 <br />
This function fails to handle anything more than one deep, as well as it doesn't handle boolean values the way PHP does.
<pre><code>
function http_build_query(formdata, numeric_prefix, arg_separator) {
var key, tmp = [],
_http_build_query_helper = function (key, val, arg_separator) {
var k, tmp = [];
if (val === true) {
val = "1";
}
else if (val === false) {
val = "0";
}
if (typeof(val) == "array" || typeof(val) == "object") {
for(k in val) {
if(val[k] !== null) {
tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));
}
}
return tmp.join(arg_separator);
}
else if(typeof(val) != "function") {
return urlencode(key) + "=" + urlencode(val);
}
};
if (!arg_separator) {
arg_separator = "&";
}
for (key in formdata) {
if (numeric_prefix && !isNaN(key)) {
key = String(numeric_prefix) + key;
}
tmp.push(_http_build_query_helper(key, formdata[key], arg_separator));
}
return tmp.join(arg_separator);
}
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-03-15 11:57:05 <br />
@ Michael White: Thanks for bringing it to my attention, yeah your mail is quite big so I saved it for this weekend ;) I'll get back to you today or tomorrow, cheers
<hr />
<strong>
Michael White
</strong>
on 2008-03-14 17:28:21 <br />
No problem. Did you get my email from a couple of days ago? Also, I left a bugfix on the print_r() function that doesn't seem to have been noticed yet.
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-03-14 08:27:49 <br />
@ Michael White: thanks for pointing that out Michael!
<hr />
<strong>
Michael White
</strong>
on 2008-03-14 02:29:20 <br />
Well I just found out that escape() is actually not the best thing to use for this function. encodeURIComponent() seems to be required here. The reason behind this is that a literal + sign does not get escaped by escape() and so gets &quot;lost in translation&quot; when working in PHP. I never would have discovered this if I had not been sending values that were either a + or - sign. I have no idea if there are any other characters affected by this at this time. My apologies to anyone who may be affected by this; I thought that escape() worked properly until now.
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-03-03 11:46:40 <br />
@ Michael White: fixed!
<hr />
<strong>
Michael White
</strong>
on 2008-03-02 21:41:03 <br />
One more little note:
Replace:
<pre><code>
use_val = use_val.replace('%20', '+');
</code></pre>
with:
<pre><code>
use_val = use_val.replace(/%20/g, '+');
</code></pre>
The original version only replaces a single instance of the string. Using the regular expression with /g at the end tells it to replace &quot;globally&quot; meaning every instance of %20 in the string.
http://crestidg.com
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-03-02 19:29:30 <br />
@ Michael White: Done
<hr />
<strong>
Michael White
</strong>
on 2008-03-02 18:37:21 <br />
Ooops - forgot the code block....
Replace....
<pre><code>
use_key = encodeURIComponent(key);
use_val = encodeURIComponent(formdata[key].toString());
use_val = use_val.replace('%20', '+');
</code></pre>
<pre><code>
use_key = escape(key);
use_val = escape(formdata[key].toString());
</code></pre>
<hr />
<strong>
Michael White
</strong>
on 2008-03-02 18:31:28 <br />
Ah - I ran into that problem where the output of encodeURI was incompatible with PHP as well about two months ago.
You have to use escape() instead. PHP likes this one. I've used it for a while in my own Ajax requests.
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-03-02 12:22:44 <br />
@ marrtins: Thanks for contributing!
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-03-02 12:15:50 <br />
Legaev Andrey: Thank you, still needed the
<pre><code>
use_val = use_val.replace('%20', '+');
</code></pre>
though, apparently encodeURIComponent does not produce php compatible encoded ouput
<hr />
<strong>
Legaev Andrey
</strong>
on 2008-03-02 07:20:24 <br />
Hi!
Key can contain no-ascii character too, and encodeURIComponent() is more appropriate function.
Please, replace code
<pre><code>
use_key = key;
use_val = encodeURI(formdata[key].toString());
use_val = use_val.replace('%20', '+');
</code></pre>
by following:
<pre><code>
use_key = encodeURIComponent(key);
use_val = encodeURIComponent(formdata[key].toString());
</code></pre>
Key can contain no-ascii character too, and encodeURIComponent() is more appropriate function.
<hr />
<strong>
marrtins
</strong>
on 2008-03-02 06:03:30 <br />
Hello!
I got syntax errors on IE6:
const CASE_LOWER = 0;
const CASE_UPPER = 1;
raplacing with
var CASE_LOWER = 0;
var CASE_UPPER = 1;
works fine
<hr />