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
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')

    )

));

 

 

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.