-1

Currently I am trying to create a live search bar that only produce 5 results max and more option if there is over 5 results. So what I have done so far is a jquery ajax script to call a php script that runs asynchronously on key up in textbox I have.

I want to get the php array then I will code it further using javascript.

This is my code now:

Javascript code
<script type="text/javascript">
    function find(value)
    {
    $( "#test" ).empty();
    $.ajax({
    url: 'searchDb.php',
    type: 'POST',
    data: {"asyn": value},
    success: function(data) {
    return $lala;
    var lala = $lala;
    $( "#test" ).html($lala);
    }
    });

    }
   </script>

SearchDb PHP code:

<?php

function searchDb($abc, $limit = null){

 if (isset($abc) && $abc) {

$sql = "SELECT testa FROM test WHERE testa LIKE '%$abc%'";


if($limit !== null){
$sql .= "LIMIT ". $limit;  

}

$result = mysql_query($sql) or die('Error, insert query failed') ;


$lists = array();
while ( $row = mysql_fetch_assoc($result))
{

  $var = "<div>".$row["testa"]."</div>";
array_push($lists, $var);
 }
 }
 return $lists;
 }

 $abc = $_POST['asyn'];
 $limit = 6;
 $lala = searchDb($abc);
 print_r($lala);

  ?>

How can I get $lala

1
  • you can get $lala if your searchDb.php page is not throwing any error, You just need to echo $lala; but what surpirse me is , why are you returning lala in Java script page? as you are still using same variable in the lower portion. Commented Jan 13, 2014 at 17:28

3 Answers 3

0

Have you considered encoding the PHP array into JSON? So instead of just echoing the array $lala, do:

echo json_encode($lala); 

Then, on the Javascript side, you'll use jQuery to parse the json.

var jsonResponse = $.parseJSON(data);

Then you'll be able to use this jsonResponse variable to access the data returned.

0

You need to read jQuery .ajax and also you must view this answer it's very important for you

$.ajax({
  url: 'searchDb.php',
  cache: false,
  type: 'post'
})
  .done(function(html) {
    $("#yourClass").append(html);
  });

In your searchDb.php use echo and try this code:

    function searchDb($str, $limit = null){
      $lists = array();
      if (isset($str) && !empty($data)) {
        $sql = "SELECT testa FROM test WHERE testa LIKE '%$data%'";
        if(0 < $limit){
          $sql .= "LIMIT ". $limit;  
        }
      $result = mysql_query($sql) or die('Error, insert query failed') ;
      while ( $row = mysql_fetch_assoc($result))
      {
         $lists[] = "<div>".$row["testa"]."</div>";
      }
   }
   return implode('', $lists);
   }

     $limit = 6;
     $data = searchDb($_POST['asyn'], $limit);
     echo $data;
 ?>
0

If you dont have or your page searchDb.php dont throw any error, then you just need to echo $lala; and you will get result in success part of your ajax function

ALso in your ajax funciton you have

    //you are using data here
   success: function(data) {
return $lala;
var lala = $lala;
$( "#test" ).html($lala);
}

you must try some thing like this

  success: function(data) {
var lala = data;
$( "#test" ).html($lala);
}
3
  • so is the data produced the array in my php code because I want to further break it down using .each jquery function to style it out before producing in the div tag Commented Jan 13, 2014 at 17:38
  • It will produce in the way you are sending it from server side, actually if you want to loop through it for styling etc i would recommend echo data in what ever style you want it to be in from server side. But if you want to use it here i would recommend using JSON. As Json will return objects which can be easily .each() using jquery and if you use JSON you have to do minimum changes in your current code Commented Jan 13, 2014 at 17:41
  • you are more then welcome mate, you can ask anything similar in this thread if you cant figure out json Commented Jan 13, 2014 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.