1

I am developing a social website like facebook in php. I have a search bar in a page named as showdetails.php which should display the name of the users (in the database) in a dropdown list like div which can be clicked for selection ,as user types the letters in the search box.I have done this using ajax.When I click a particular user name in the dropdown list, it automatically comes to the search box.My issue is that there is another div which needs to display the details of the selected user clicked by the user.I have searched a lot but couldnt get the correct one.I am unaware of returning two different values from a ajax call since in my knowledge var response = xmlhttp.responseText;,the variable response can store only one result.

My searchvalues.phpwhich contains the code of searching the username

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

My javascript functions in showdetails.php

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

3 回答 3

6

//服务器端:

$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);
    }
});
于 2013-01-30T06:35:49.387 回答
0

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

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

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

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

别的

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

将 .ajax() 与 JSONP 一起使用的基本示例?

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

于 2013-01-30T06:13:54.300 回答
0
<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 中显示的信息

于 2013-01-30T08:07:58.150 回答