Hi guys, In PHP, I have an array which look like this: $arr = ['x' => [$a,$b]]; Code (markup): How do I write this in JavaScript? Thanks,
Such operations like below seem to be okay in javascript: var arr = []; arr['x'] = ['hello', 'world']; Code (JavaScript): ? Hendra
Wooh! I use it like this: var xhr = new XMLHttpRequest(); xhr.open('GET', 'index.php?x[0]=' + var_a + '&x[1]=' + var_b, true); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var msg = JSON.parse(xhr.responseText); } } Code (markup): To solve ajax request with link like this: <a href="index.php?<?php echo http_build_query(['x' => [$a,$b]]); ?>">Get Data</a> Code (markup):
Hey that's new to me; the http_build_query(). Thx . It seems like you need to process the $arr['x'] in client's browser javascript first before sending the ajax, right? Else, a direct use of the http_build_query() might simplify the steps a bit, I guess: xhr.open('GET', 'index.php?<?php echo http_build_query(["x" => [$a,$b]]); ?>', true); PHP: Hmm, doesn't look nice though
It looks nice. With this, you can encrypt data both in PHP and JavaScript, making it harder a bit for hacker to attack. Ajax can't do something like this if you use the js file: xhr.open('GET', 'index.php?<?php echo http_build_query(["x" => [$a,$b]]); ?>', true); Code (markup):
No no no, Javascript is not like php, you couldn't use associative array, you should use object. var arr = {}; arr['x'] = ['hello', 'world']; Code (JavaScript):
Thanks for pointing that out. I'll go back and have it corrected. Do you have any good links which I can read about? My website is JavaScript heavy, sort of.
https://docs.oracle.com/cd/E19957-01/816-6408-10/object.htm#1193229 Beware that ECMA-262 is an old reference. It was the one though which started me to understand objects, arrays and their differences I'm sure you'll easily find better references, e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object Hendra