0

Problem
I am using AJAX jQuery with a dropdown to get some response from PHP. So far i wanted just one row from database, but now i want another array.

Current situation

front -

    $.ajax({
    type: "POST",    
    url: "project_details.php",    
    data: data_string,
    cache: false,
    success: function(data){
    if(data){
    var json = data;
    obj = JSON.parse(json);
    $("#project-name").text(obj.project_name);
    $("#start-date").text(obj.start_date);
    }
    }
    });

back -

$result=mysqli_query($db,"SELECT distinct project_name,start_date FROM `projects` WHERE a.supervisor_email = '$email' and a.project_id = '$project'"); 
$count=mysqli_num_rows($result);

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

if($count==1){ 
echo json_encode(array("project_name" =>$row['project_name'],
"start_date" => $start->format("d M,Y"))); }

What I want - I need another array returned from PHP -

$result_1=mysqli_query($db,"SELECT member_email FROM `projects` WHERE a.supervisor_email
$email' and a.project_id = '$project'"); 

$row_1=mysqli_fetch_array($result,MYSQLI_ASSOC);

so the final echo should be something like

if($count==1){ 
echo json_encode(array($row_1,"project_name" =>$row['project_name'],
"start_date" => $start->format("d M,Y"))); }

I can't figure out how to read this from jQuery

Note that table I'm using is at project_id, member_email level

EternalHour
  • 8,308
  • 6
  • 38
  • 57
user3804483
  • 117
  • 1
  • 2
  • 8

3 Answers3

1

First of all, specify the datatype as json.

This way you do not need..

var json = data;
obj = JSON.parse(json);

..and you can use the data variable directly. Also, depending on what you are doing with the AJAX data, it may be better to use .html() instead of .text().

In regards to your original question, I see that you have added $row_1 to your existing array but it will not work that way. I don't know what it contains, but seems to be an array. Since AJAX is expecting json format you need to have key=>value pairs. How about like this?

PHP:

$json_arr = array();

while ($row_1=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $email{'member_email'] = $row_1['member_email'];
}
if($count==1){ 
    echo json_encode(array("member_email" =>$email,
    "project_name" =>$row['project_name'],
    "start_date" => $start->format("d M,Y"))); 
}

AJAX:

$.ajax({
    type: "POST",    
    url: "project_details.php",    
    data: data_string,
    dataType: "json",
    cache: false,
    success: function(data){
        if(data){
            $("#member_email").text(data.member_email);
            $("#project-name").text(data.project_name);
            $("#start-date").text(data.start_date);
        }
    }
});
EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • This is working fine. The problem comes when i try to return another array of member email having same project id and supervisor. I can't figure out how to return the array $row_1 with same echo. Please check the last few lines of my question. – user3804483 Dec 10 '14 at 07:52
  • I see, you've just added `$row_1` to the existing array. You cannot send it this way. See my updated answer. – EternalHour Dec 10 '14 at 07:59
  • @user3804483 is `$row_1` an array or single value? Seems that it is an array and you will need to reference it as key=>value like you did with the other elements. – EternalHour Dec 10 '14 at 08:05
  • $row_1 is an array with member email. Say there are 4 members in a particular project. I need all of their emails, but i can't know beforehand how many members will there be. Is there a way to write a loop in php for jQuery to get all values from the array as a part of same echo? – user3804483 Dec 10 '14 at 08:15
  • You will have to do the loop in javascript. – EternalHour Dec 10 '14 at 08:25
0

Do you get what you want when you do a console.log(obj); ?

Cause there is no "project_name" inside your SQL-Select

Regarding jQuery .. tried $.each?

$.each(obj, function(key, project)
{
    ...text(project.project_name);
});
Ferret
  • 1,440
  • 2
  • 12
  • 17
0

I'm not exactly sure if I got your question right. But you would like to have $row_1 as additional array to be echoed, am i right? If so, Try this:

PHP

echo json_encode( array(
                 "row_1" => $row_1,
                 "project_name" =>$row['project_name'],
                 "start_date" => $start->format("d M,Y")
               )
);

Then on your $.ajax use data.row_1 , data.project_name, data.start_date to refer to your encoded value

Note: I would also like to recommend what @EternalHour said that you should use dataType: 'json' instead for a much cleaner approach, and it seems to be easier that way too.

Please tell me if my recommended code worked. If not would you please tell me the error that comes with it too.

Arvs
  • 312
  • 1
  • 4