Jump to content

AJAX Using JSON, how do I read the object?


JustinK101

Recommended Posts

So I have my php script, which is as follows:

 

<?php
require("pref.php");
require("db_connect.php");

if(!empty($_GET['customer_id']) && $_GET['customer_id'] != "None")
{
	mysql_select_db("yjpalmer_ordermanager");
	$sql = "SELECT order_id FROM orders WHERE associated_customer_id = " . $_GET['customer_id'] . " ORDER BY order_id ASC";
	$result = mysql_query($sql);

	$myArray = array();
	while($row = mysql_fetch_row($result))
	{
		array_push($myArray, $row[0]);
	}

	print(json_encode($myArray));
}
?>

 

A test output of the above script is:

 

["263","285","286","287","288","290","291","292","293","294"]

 

Then my Javascript code is:

 

function updateDependentSelectBox(serverSideScript, objectToRead, queryStringName, objectToAppend)
{	
var the_obj;
document.getElementById('ajax_status').innerHTML = '<img src="includes/ajax.gif" alt="Please Wait...">';
xmlHttp = createAjaxObject();
myValue = objectToRead.value;
xmlHttp.open("GET", serverSideScript + '?' + queryStringName + '=' + myValue + '&sid=' + Math.floor(Math.random() * 1000000), false);

xmlHttp.onreadystatechange=function()
   	{
	if(xmlHttp.readyState==4)
	{
		if (xmlHttp.status == 200)
		{
			the_obj = eval("(" + xmlHttp.responseText + ")");
			for(i = 0; i < the_obj.length; i++)
			{
				objectToAppend.options[objectToAppend.length] = new Option(the_obj[i], false, false);
			}
			setTimeout('document.getElementById(\'ajax_status\').innerHTML = \'\'', 850);
		}
		else 
		{
            	alert("AJAX Error! There Was A Problem Parsing The Following Server Side PHP Script.\n\n" + serverSideScript + "");
		}
        }
};

xmlHttp.send(null);		
}

 

My problem is the following line:

 

the_obj = eval("(" + xmlHttp.responseText + ")");

 

What does eval do? How is the variable the_obj constructed? How do I get my data out of it?

Link to comment
Share on other sites

What does eval do?

It executes the text passed to it as javascript.

 

http://www.w3schools.com/jsref/jsref_eval.asp

 

How is the variable the_obj constructed?

It's defined at the beginning of execution:

 

var the_obj;

 

It is assigned a value of the result of the eval function.  Then it is added as an option in a select box.

 

How do I get my data out of it?

This script that you are using will not do anything with JSON...it executes javascript returned from the server and appends the result to a select box option group.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.