DolDurma
DolDurma

Reputation: 17360

send array with JSON

i'm using this code for post text of option tag :

$("#major_names").change(function () {
    $.post('find_lesson.php',  { dars:$("#major_names option:selected").text() },
        function(data){ 
            if (data.success)
                        $("div").append(data.doroos);
            else
                alert('mm');
        },'json'); 
return false; 
});

now in find_lesson file i use this code for fetch any record from database

<?php

$lessonName=$_POST['lesson '];
$query= mysql_query("SELECT * FROM at_*** WHERE title = '{$lessonName}'");
$result= mysql_fetch_array($query);

$sql= mysql_query("SELECT * FROM  *** JOIN at_lessons ON  . . .");
while($result=mysql_fetch_assoc($sql))
{
$data ['doroos']= $result['title'];
}
$data['success']=true;


echo json_encode($data);

?>

mysql command is correct but after send array i have getting 1 record. please help me

Upvotes: 0

Views: 147

Answers (2)

retromuz
retromuz

Reputation: 799

You should do something like below instead. Otherwise "doroos" will only have the last title record from the fetched resultset.

<?php

$lessonName=$_POST['lesson '];
$query= mysql_query("SELECT * FROM at_*** WHERE title = '{$lessonName}'");
$result= mysql_fetch_array($query);

$sql= mysql_query("SELECT * FROM  *** JOIN at_lessons ON  . . .");

$doroos = array();
$x = 0;
while($result=mysql_fetch_assoc($sql))
{    
     $doroos[x++] = $result['title'];
}
$data ['doroos']= $doroos;
$data['success']=true;


echo json_encode($data);

?>

Upvotes: 0

Prasanth
Prasanth

Reputation: 5268

Change

$data ['doroos']= $result['title'];

to

$data ['doroos'][] = $result['title'];

You are basically overwriting the doroos. Instead, use [] to add every $result['title'] to doroos.

Upvotes: 5

Related Questions