繁体   English   中英

在ajax中返回多个数据

[英]return multiple data in ajax

我正在用php开发一个类似facebook的社交网站。 我在名为showdetails.php的页面中有一个搜索栏,该页面应在dropdown list like div which can be clicked for selectiondropdown list like div which can be clicked for selection显示用户名(在数据库中) dropdown list like div which can be clicked for selection用户在搜索框中键入字母时dropdown list like div which can be clicked for selection 。我使用ajax完成此操作。当我单击下拉列表中的特定用户名时,它会自动出现在搜索框中。我的问题是还有一个div需要显示用户单击的所选用户的详细信息。我已经搜索了很多,但是没有找到正确的值。我不知道从ajax调用返回两个不同的值,因为据我所知var response = xmlhttp.responseText; ,可变响应只能存储一个结果。

我的searchvalues.php包含搜索用户名的代码

       $hint = "<table>";
       while($row = mysql_fetch_array($result_query_get_following))
     {
         $act_fname = $row['acnt_fname'];
         $act_lname = $row['acnt_lname'];
         $act_name = $act_fname . ' ' . $act_lname;
         $hint.= "<tr onClick='showVal(\"".$act_name."\");'><td >".$act_name."</td></tr>";
         $following_id = $row['flwing_following_user_id'];
         $following_member_class = $row['acnt_member_class'];
         $following_profile_picture = $row['acnt_profile_picture'];
    }
     $hint .= "</table>";
}
  if ($hint == "")
  {
$response="no suggestion";
   }
 else
 {
$response=$hint;
 }

//output the response
echo $response;

我在showdetails.php中的javascript函数

   function showVal(val)
{
    document.getElementById("quicksearch").value=val;
    document.getElementById("search").style.display="none";
}

//服务器端:

$return_data['status'] = '0';

$return_data['msg'] = 'Your message.'; 

echo json_encode($return_data);exit;

// 客户端

$.ajax({
    dataType: "json",
    type: "POST",
    url: 'Your url',
    data: ({parm1:value1,...}),
    success: function (data, textStatus){
        $("#div1").html(data.msg);
        $("#input1").val(data.status);
    }
});

据我所知,从函数返回两个元素是不可能的。 您可以做的是创建一个元素数组并返回该特定数组。

EG: 鉴于这是PHP,因为您未提供任何代码

function getXYZ()
{
    return array(1,2,3);
}

list($x,$y,$z) = getXYZ();

其他

您可以尝试使用JSON返回数据。

在JSONP中使用.ajax()的基本示例?

http://www.jquery4u.com/json/ajaxjquery-getjson-simple/

<div id="user_detail"></div>

    <script>
    $("#clicked_user").click(function(){
        var q =  document.getElementById('clicked_user').value;
        var loadUrl = "getuserinformation.php?query=q";
        $.post
        (
          loadUrl,{language: "php", version: 5},function(responseText){$("#user_detail").html(responseText);},"html"
        );
    })
    </script>

getuserinformation.php中 ,通过$ _get获取查询参数,在此处进行查询并回显要在div中显示的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM