SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    json - driving me crazy! Yes, I've read through the docs.

    Clearly, I'm doing something wrong but I've been working on this all day and for some reason can't get it to work. I've gone through http://api.jquery.com/jQuery.post/ backwards and forwards, any help is greatly appreciated!

    jQuery:

    Code JavaScript:
    $(function () {
    	$('#vehicleModel').change(function() {
    		$.post("ajax.php", { vehicleModel:$('#vehicleModel').val() },
    		function(data){
    			alert(data.optionValue);
    			$('#vehicleYear option').remove();
    			$.each(data,function(i,item) {
    				$('#vehicleYear').append('<option value="'+item.optionValue+'">'+item.optionDisplay+'</option>');
    			});
    		}, "json");
    	});
    });

    PHP:

    Code PHP:
      $Model_t = $_REQUEST['vehicleModel'];
     
      $results = mysql_query('select * from vehicle_applications where Model_t="' . $Model_t . '"');
     
      $json = array();
     
      while (is_resource($results) && $row = mysql_fetch_object($results)) {
          $Year = strtoupper($row->Year_t);
          $Model = strtoupper($row->Model_t);
     
          $json[] = '{optionValue: "'.$Model.'", optionDisplay: "'.$Year.'"}';
      }
     
      header('Content-type: application/json'); 
      echo '[' . implode(',', $json) . ']';
      die();

    My response in firebug looks like:

    Code JavaScript:
    [{optionValue: "FORD CAR", optionDisplay: "41-48"},{optionValue: "FORD CAR", optionDisplay: "32"},{optionValue: "FORD CAR", optionDisplay: "33-34"}]

    I get nuthin' from the alert but firebug says the json n/v is there.

  2. #2
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your JSON is not valid JSON.




    Try this:

    Code PHP:
    $json[] = '{"optionValue": "'.$Model.'", "optionDisplay": "'.$Year.'"}'; // notice the double quotes in the keys

    See: http://json.org/
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AAAARRRGGGGHHH!!!! Thank you so much, I could have SWORN I tried formatting that way. The version I posted here just happened to be one of the last (of many) formatting efforts I made.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of manually making json format, how about using json_encode() function:

    http://us2.php.net/manual/en/function.json-encode.php
    Follow me @YaderHernandez


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •