Jump to content

More JSON problems now with json_encode


c_shelswell

Recommended Posts

Ok so i've take up previous advice and tried to use json.encode with my data but now all I get returning to my select box is 'undefined'. I know i'm getting results from my mysql query it just my select box isn't populating with the data.

 

my code is:

foreach ($cats as $k => $v)
{
	$arr["optionValue: ".$v['id'].""] = "optionDisplay: '".$v[DEFAULT_LANG]."'";
}


echo "[".json_encode($arr,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP)."]";

 

Then the javascript on the page with the select box is:

$(function()
    {		 
	$("select#membersOrPublic").change(function()
	 {
		$.getJSON("get_categories.php",{id: $(this).val(), ajax: 'true'}, function(j)
		{
			var options = '';
			for (var i = 0; i < j.length; i++) 
			{
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			    
			    if (j[i].optionValue == 0)
			    {
				  // no results have been returned
			    }

			}


			$("select#categoryList").html(options);
		})

	})
})

 

I'm afraid I'm not overly familiar with JSON so finding this one quite difficult to problem solve

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/173279-more-json-problems-now-with-json_encode/
Share on other sites

Yes, that's formatted incorrectly.

 

AJAX is basically just an object, and object notation is:

 

{"key": "value", "key2": "value2"}

 

 

So, you'll need to make it match that.

 

 

Here's an example:

 

 

echo json_encode(array(

    'people' => array(

        array('name' => 'Corbin', 'hobby' => 'PHP'),

        array('name' => 'John', 'hobby' => 'Something else')

    )

));

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.