无法将json数组从PHP返回到jQuery AJAX [英] Can't return json array from PHP to jQuery AJAX

查看:96
本文介绍了无法将json数组从PHP返回到jQuery AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题
我正在使用带有下拉菜单的AJAX jQuery,以从PHP获得一些响应.到目前为止,我只想从数据库中获取一行,但是现在我想要另外一个数组.

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.

当前情况

前面-

    $.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);
    }
    }
    });

后退-

$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"))); }

我想要的东西- 我需要从PHP返回的另一个数组-

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

所以最终的回声应该是

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

我不知道如何从jQuery读取

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

请注意,我正在使用的表位于project_id,member_email级别

推荐答案

我不确定我是否正确回答了您的问题.但是,您希望将$ row_1作为要回显的其他数组,对吗?如果是这样,请尝试以下操作:

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")
               )
);

然后在$ .ajax上 使用data.row_1,data.project_name,data.start_date引用您的编码值

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

注意:我也想推荐@EternalHour所说的,您应该使用dataType:'json'代替,这是更简洁的方法,而且这样看起来也更容易.

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.

这篇关于无法将json数组从PHP返回到jQuery AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆