-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

  • 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. – noobie-php Jan 13 '14 at 17:28

3 Answers3

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);
}
noobie-php
  • 6,817
  • 15
  • 54
  • 101
  • 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 – user3104737 Jan 13 '14 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 – noobie-php Jan 13 '14 at 17:41
  • you are more then welcome mate, you can ask anything similar in this thread if you cant figure out json – noobie-php Jan 13 '14 at 18:20
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;
 ?>
Community
  • 1
  • 1
RDK
  • 4,540
  • 2
  • 20
  • 29
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.

John D.
  • 2,521
  • 3
  • 24
  • 45