views:

526

answers:

4

Hey people :-)

I'm trying to load some data via. jquery

 $get_desc = "SELECT tekst_id,overskrift FROM rr_tekster WHERE beskrivelse_id = '". $_POST['beskrivelse_id'] ."' AND status = 1";
 $select = mysql_query($get_desc)or die(mysql_error());
 while($row_option = mysql_fetch_array($select)){
  $output .= '<option value="'.$row_option['tekst_id'].'">'.$row_option['overskrift'].'</option>';
 } 


$arr = array (
  'list_options' => $output
);
echo json_encode($arr);

and my jquery looks like this

$.post(action, { beskrivelse_id:des_id }, function(data){           
       $(load_div).fadeOut();
       $(result).html('<select name="tekster">'+data.list_options+'</select>').fadeIn(500);

     },'json');

THE "ERROR": {"list_options":null}

in firebug it gives me an null error in the output... i want it to show all rows in my database, rigtig now it should show me 3 rows..

but nothing .. :-/ why ?

A: 

You don't need json for what you are trying to achieve:

$get_desc = "SELECT tekst_id,overskrift FROM rr_tekster WHERE beskrivelse_id = '". $_POST['beskrivelse_id'] ."' AND status = 1";
        $select = mysql_query($get_desc)or die(mysql_error());
        while($row_option = mysql_fetch_array($select)){
                $output .= '<option value="'.$row_option['tekst_id'].'">'.$row_option['overskrift'].'</option>';
        }       

echo $output;


$.post(action, { beskrivelse_id:des_id }, function(data){                                                               
                                                        $(load_div).fadeOut();
                                                        $(result).html('<select name="tekster">'+data+'</select>').fadeIn(500);

                                        });
karim79
yeah, that seems to work ... :o Now i just got some charset error/ bugs... :-/its kinda of weird, cause I always used json_encode but this time it did't work.. spooky ...
william
A: 

You seem to be JSON-encoding an array, which would look like [ ] in the JSON-syntax. However, you're accessing it with the object-syntax (data.list_options). You could do:

$responseobj = new stdClass();
$responseobj->list_options = $output;
echo json_encode($responseobj);
JorenB
A: 

$output is NULL when your query returns no result, and you (not good!) not initialize $output at the beggining and you not check, if $output is empty or not in PHP.

Thinker
A: 

I had problems with json_encode. I could only get it to encode classes, so I had to instantiate an object and json_encode that object to return my data.

Jim Schubert