MySQL PHP associative array to Javascript via JSON + jQuery

MySQL PHP associative array to Javascript via JSON + jQuery

Problem:
Your table-driven application has a MySQL table on the server side and you want a Javascript associative array on the client side to do rapid look-ups.
For example: My application uses the value selected from the form’s drop-down menu to choose which other form fields to hide or show. This choice of a 'Long Form' is table-driven from product data in a MySQL table.

Solution:
Convert the MySQL table to an associative array in JavaScript.

I am using the jQuery library to manage the presentation and form validation on the client side and PHP and MySQL on the server side.

Since a JavaScript Object is an associative array and the MySQL table is being used as a look-up table (associative array) I use the PHP function json_encode to create a JSON string - JSON is JavaScript Object Notation - on the server which is then evaluated as a JavaScript Object on the client.

The two parts (server and client) of the code are listed below.

All very straightforward then. So why the blog? Well, I got some poor advice which wasted me some time. Hopefully this experience will save you some time.

This is what I do (here's the PHP code):
do {    $key = $row['ProductKind'];
   $value = ($row['LongForm'] ? True : False);
   $arr[$key] = $value;
} while ($row = mysql_fetch_assoc($rs));

echo json_encode($arr);


That’s the server side. Now, on the client side, in the jQuery style:
$(document).ready(
   function() {   
      var AK = new Object;
      $.post("php/echoAK.php",jsResponse,"json");
      function jsResponse (data){
         AK = JSON.parse(data);
         } // end function jsResponse
      } // end anon function for ready
   ); // end ready


Now I can use AK as follows:
if (AK.MyProduct){..}

As a newcomer to jQuery this is a good opportunity to show my appreciation to John Resig and the people that have contributed to the jQuery library ... not forgetting those who have integrated all the MySQL and JSON stuff into PHP. It's all very neat ... when you get to understand it!

There are several other issues which you might like to note, when using this approach:
You could just eval(data) to turn the JSON string into a JavaScript Object (a.k.a. associative array), but JSON.parse protects your code against evaluating malicious scripts that could be embedded in the JSON string. This function is found in the json2.js downloadable until included in native browser JavaScript engines.
The PHP Booleans translate automatically into JavaScript Boolean types.

[Skip this: My application has a MySQL table of product categories that tell me whether I need to present the administrator with a long form or a short form in the HTML. So, as products are added over time, I want to the administrator to choose whether a new product category can take the short form plus some default values or will require the explicit input of all product values in the long form. As the administrator assigns a category to a new product - and changes his mind half a dozen times - I don’t want to keep on going to the server to do the look-up but want to have the table in Javascript to keep the form presentation responsive. Also, the MySQL tables are on a third party’s server remote from the web server.]