View Full Version : SQL Query, JSON_ENCODE, and JavaScript variables

01-25-2011, 03:37 PM
I have an array from a sql query that I need to use as variables in a javascript function. I have created the array, used JSON_ENCODE to bring the information into javascript, and so far everything works correctly.

I am trying to use a list menu in html to select one on the indexes of the array so I can pass the information from the index to a javascript function.

Here is the javascript function:

function selectTable(item_id)
var media = eval(<? echo json_encode($paperTable); ?>);
var click_k_1=media[item_id]["click_k_1"];
var click_k_501=media[item_id]["click_k_501"];
var click_c_1=media[item_id]["click_c_1"];
var click_c_51=media[item_id["click_c_51"];
var click_c_101=media[item_id]["click_c_101"];
Here is the list menu coding:

<select name="pprSel" size="1" id="pprSel" onChange="selectTable(this.value);" >
<option value="204226">20# Bond</option>
<option value="204788">70# White</option>
<option value="204823">80# White</option>
Here is the source file results from the browser:

function selectTable(item_id)
var media = eval({"1":{"item_id":"204226","item_description":"20# Bond","click_k_1":"0.1100","click_k_501":"0.0730","click_c_1":null,"click_c_51":null,"click_c_101":null},"2":{"item_id":"204788","item_description":"70# White","click_k_1":"0.1149","click_k_501":"0.0779","click_c_1":"0.8789","click_c_51":"0.5539","click_c_101":"0.3589"},"3":{"item_id":"204823","item_description":"80# White","click_k_1":"0.1591","click_k_501":"0.1221","click_c_1":"0.9231","click_c_51":"0.5981","click_c_101":"0.4031"}});
I have tried a couple of different ways to pass one of the variables to a text field on the html form as an error check, with no success:

The text field on the html form is id='data', the form is id='quote'.

the only thing I can think of for the reason I am not getting anything in my text field is that my variable is not getting set up properly.

Can anyone help? TIA

01-25-2011, 05:05 PM
The chained DOM notation is expecting name attributes instead of id attributes



<form name="myform" action="..." method="...">
<input type="text" name="mytext" value="" />


var myField = document.myform.mytext;

rnd me
01-25-2011, 08:02 PM
kill the eval, you don't need it when you deliver literal objects without using ajax.