PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/_octopress/source/functions/http_build_query/_comments.html

https://gitlab.com/orvi2014/phpjs
HTML | 377 lines | 296 code | 80 blank | 1 comment | 0 complexity | e176a5b036798292b6716e04442ed2cc MD5 | raw file
  1. <!-- Generated by Rakefile:build -->
  2. <strong>
  3. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  4. </strong>
  5. on 2012-06-16 08:17:54 <br />
  6. @Click Button Publishing: Glad to hear it is working for you! (I'm trying to help catch up on old messages one-by-one.)
  7. <hr />
  8. <strong>
  9. <a href="www.olivagreca.com" rel="nofollow">Tom </a>
  10. </strong>
  11. on 2012-05-12 11:04:41 <br />
  12. Hello guys,
  13. I am trying to produce a query string like:
  14. //localhost:8080/select/?q=patata&amp;model:journal+model:member+model:new_member+model:book
  15. But when i am using the below code block i have an output like:
  16. //localhost:8080/select/?q=patata&amp;model:journal
  17. How can i use repetitive pair values?
  18. Thank you in advance
  19. <pre><code>
  20. &lt;script type=&quot;text/javascript&quot;&gt;
  21. var a = &quot;member&quot;;
  22. var b = &quot;new_member&quot;;
  23. var c = &quot;book&quot;;
  24. var d = &quot;journal&quot;;
  25. var e = &quot;cds&quot;;
  26. var f = &quot;pdfs&quot;;
  27. function http_build_query (formdata, numeric_prefix, arg_separator) {
  28. var value, key, tmp = [],
  29. that = this;
  30. var _http_build_query_helper = function (key, val, arg_separator) {
  31. var k, tmp = [];
  32. if (val === true) {
  33. val = &quot;1&quot;;
  34. } else if (val === false) {
  35. val = &quot;0&quot;;
  36. }
  37. if (val != null) {
  38. if(typeof(val) === &quot;object&quot;) {
  39. for (k in val) {
  40. if (val[k] != null) {
  41. tmp.push(_http_build_query_helper(key + &quot;[&quot; + k + &quot;]&quot;, val[k], arg_separator));
  42. }
  43. }
  44. return tmp.join(arg_separator);
  45. } else if (typeof(val) !== &quot;function&quot;) {
  46. return that.urlencode(key) + &quot;:&quot; + that.urlencode(val);
  47. } else {
  48. throw new Error('There was an error processing for http_build_query().');
  49. }
  50. } else {
  51. return '';
  52. }
  53. };
  54. if (!arg_separator) {
  55. arg_separator = &quot;&amp;&quot;;
  56. }
  57. for (key in formdata) {
  58. value = formdata[key];
  59. if (numeric_prefix &amp;&amp; !isNaN(key)) {
  60. key = String(numeric_prefix) + key;
  61. }
  62. var query=_http_build_query_helper(key, value, arg_separator);
  63. if(query != '') {
  64. tmp.push(query);
  65. }
  66. }
  67. return tmp.join(arg_separator);
  68. }
  69. function urlencode (str) {
  70. str = (str + '').toString();
  71. // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
  72. // PHP behavior, you would need to add &quot;.replace(/~/g, '%7E');&quot; to the following.
  73. return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
  74. replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
  75. }
  76. var aa = http_build_query({'model': a, 'model': b, 'model': c, 'model': d}, '', '+');
  77. var value= &quot;patata&amp;&quot;;
  78. var qurl = &quot;//localhost:8080/select/?q=&quot; + value + aa
  79. console.log(qurl);
  80. &lt;/script&gt;
  81. </code></pre>
  82. <hr />
  83. <strong>
  84. <a href="140plus.2et.in" rel="nofollow">Click Button Publishing</a>
  85. </strong>
  86. on 2011-11-07 12:43:14 <br />
  87. We are using this code to format our URL and it is superb. Thanks phpJS :)
  88. <hr />
  89. <strong>
  90. Dreamer
  91. </strong>
  92. on 2010-10-23 11:41:22 <br />
  93. @Brett Zamir: It works now.
  94. <hr />
  95. <strong>
  96. <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a>
  97. </strong>
  98. on 2010-10-22 22:04:04 <br />
  99. @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...
  100. <hr />
  101. <strong>
  102. Dreamer
  103. </strong>
  104. on 2010-10-22 10:33:11 <br />
  105. Bug report: this.urlencode is not a function
  106. Related to php.default.namespaced.min.js version 3.19
  107. <pre><code>
  108. var $P = new PHP_JS();
  109. $P.http_build_query({foo: 'bar'});
  110. </code></pre>
  111. <hr />
  112. <strong>
  113. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  114. </strong>
  115. on 2009-04-14 12:02:53 <br />
  116. Alright! Nice! Saved it in SVN and will be online &amp; packaged shortly
  117. <hr />
  118. <strong>
  119. stag019
  120. </strong>
  121. on 2009-04-05 20:20:16 <br />
  122. D'oh.
  123. Change line 2 from &quot;var key, tmp = [],&quot; to &quot;var value, key, tmp = [],&quot;.
  124. Add &quot;value = formdata[key];&quot; under line 26.
  125. Change line 30 (or new line 31 if you've added the previous line from &quot;tmp.push(_http_build_query_helper(key, formdata[key], arg_separator));&quot; to &quot;tmp.push(_http_build_query_helper(key, value, arg_separator));&quot;.
  126. That should fix'er.
  127. <hr />
  128. <strong>
  129. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  130. </strong>
  131. on 2009-04-03 13:22:28 <br />
  132. @ stag019: Nice! But I can only replace the current with your implementation if it doesn't break the second example (it does now).
  133. <hr />
  134. <strong>
  135. stag019
  136. </strong>
  137. on 2009-03-30 03:43:46 <br />
  138. This function fails to handle anything more than one deep, as well as it doesn't handle boolean values the way PHP does.
  139. <pre><code>
  140. function http_build_query(formdata, numeric_prefix, arg_separator) {
  141. var key, tmp = [],
  142. _http_build_query_helper = function (key, val, arg_separator) {
  143. var k, tmp = [];
  144. if (val === true) {
  145. val = &quot;1&quot;;
  146. }
  147. else if (val === false) {
  148. val = &quot;0&quot;;
  149. }
  150. if (typeof(val) == &quot;array&quot; || typeof(val) == &quot;object&quot;) {
  151. for(k in val) {
  152. if(val[k] !== null) {
  153. tmp.push(_http_build_query_helper(key + &quot;[&quot; + k + &quot;]&quot;, val[k], arg_separator));
  154. }
  155. }
  156. return tmp.join(arg_separator);
  157. }
  158. else if(typeof(val) != &quot;function&quot;) {
  159. return urlencode(key) + &quot;=&quot; + urlencode(val);
  160. }
  161. };
  162. if (!arg_separator) {
  163. arg_separator = &quot;&amp;&quot;;
  164. }
  165. for (key in formdata) {
  166. if (numeric_prefix &amp;&amp; !isNaN(key)) {
  167. key = String(numeric_prefix) + key;
  168. }
  169. tmp.push(_http_build_query_helper(key, formdata[key], arg_separator));
  170. }
  171. return tmp.join(arg_separator);
  172. }
  173. </code></pre>
  174. <hr />
  175. <strong>
  176. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  177. </strong>
  178. on 2008-03-15 11:57:05 <br />
  179. @ 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
  180. <hr />
  181. <strong>
  182. Michael White
  183. </strong>
  184. on 2008-03-14 17:28:21 <br />
  185. 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.
  186. <hr />
  187. <strong>
  188. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  189. </strong>
  190. on 2008-03-14 08:27:49 <br />
  191. @ Michael White: thanks for pointing that out Michael!
  192. <hr />
  193. <strong>
  194. Michael White
  195. </strong>
  196. on 2008-03-14 02:29:20 <br />
  197. 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 &amp;quot;lost in translation&amp;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.
  198. <hr />
  199. <strong>
  200. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  201. </strong>
  202. on 2008-03-03 11:46:40 <br />
  203. @ Michael White: fixed!
  204. <hr />
  205. <strong>
  206. Michael White
  207. </strong>
  208. on 2008-03-02 21:41:03 <br />
  209. One more little note:
  210. Replace:
  211. <pre><code>
  212. use_val = use_val.replace('%20', '+');
  213. </code></pre>
  214. with:
  215. <pre><code>
  216. use_val = use_val.replace(/%20/g, '+');
  217. </code></pre>
  218. The original version only replaces a single instance of the string. Using the regular expression with /g at the end tells it to replace &amp;quot;globally&amp;quot; meaning every instance of %20 in the string.
  219. http://crestidg.com
  220. <hr />
  221. <strong>
  222. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  223. </strong>
  224. on 2008-03-02 19:29:30 <br />
  225. @ Michael White: Done
  226. <hr />
  227. <strong>
  228. Michael White
  229. </strong>
  230. on 2008-03-02 18:37:21 <br />
  231. Ooops - forgot the code block....
  232. Replace....
  233. <pre><code>
  234. use_key = encodeURIComponent(key);
  235. use_val = encodeURIComponent(formdata[key].toString());
  236. use_val = use_val.replace('%20', '+');
  237. </code></pre>
  238. <pre><code>
  239. use_key = escape(key);
  240. use_val = escape(formdata[key].toString());
  241. </code></pre>
  242. <hr />
  243. <strong>
  244. Michael White
  245. </strong>
  246. on 2008-03-02 18:31:28 <br />
  247. Ah - I ran into that problem where the output of encodeURI was incompatible with PHP as well about two months ago.
  248. You have to use escape() instead. PHP likes this one. I've used it for a while in my own Ajax requests.
  249. <hr />
  250. <strong>
  251. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  252. </strong>
  253. on 2008-03-02 12:22:44 <br />
  254. @ marrtins: Thanks for contributing!
  255. <hr />
  256. <strong>
  257. <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
  258. </strong>
  259. on 2008-03-02 12:15:50 <br />
  260. Legaev Andrey: Thank you, still needed the
  261. <pre><code>
  262. use_val = use_val.replace('%20', '+');
  263. </code></pre>
  264. though, apparently encodeURIComponent does not produce php compatible encoded ouput
  265. <hr />
  266. <strong>
  267. Legaev Andrey
  268. </strong>
  269. on 2008-03-02 07:20:24 <br />
  270. Hi!
  271. Key can contain no-ascii character too, and encodeURIComponent() is more appropriate function.
  272. Please, replace code
  273. <pre><code>
  274. use_key = key;
  275. use_val = encodeURI(formdata[key].toString());
  276. use_val = use_val.replace('%20', '+');
  277. </code></pre>
  278. by following:
  279. <pre><code>
  280. use_key = encodeURIComponent(key);
  281. use_val = encodeURIComponent(formdata[key].toString());
  282. </code></pre>
  283. Key can contain no-ascii character too, and encodeURIComponent() is more appropriate function.
  284. <hr />
  285. <strong>
  286. marrtins
  287. </strong>
  288. on 2008-03-02 06:03:30 <br />
  289. Hello!
  290. I got syntax errors on IE6:
  291. const CASE_LOWER = 0;
  292. const CASE_UPPER = 1;
  293. raplacing with
  294. var CASE_LOWER = 0;
  295. var CASE_UPPER = 1;
  296. works fine
  297. <hr />