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";
}
Techy
  • 2,626
  • 7
  • 41
  • 88

3 Answers3

6

//Server side:

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

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

echo json_encode($return_data);exit;

// client side

$.ajax({
    dataType: "json",
    type: "POST",
    url: 'Your url',
    data: ({parm1:value1,...}),
    success: function (data, textStatus){
        $("#div1").html(data.msg);
        $("#input1").val(data.status);
    }
});
Vijay Choudhary
  • 848
  • 1
  • 9
  • 18
0

According to my knowledge return two element from a function is not possible. What you can do is create an element array and return that particular array.

EG : Given that this is PHP, Since you have not provided any code

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

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

Else

you can try JSON to return data.

Basic example of using .ajax() with JSONP?

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

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243
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>

in getuserinformation.php get the query parameter via $_get, do query here and echo the information you want to be shown inside your div

ram obrero
  • 197
  • 10