Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a live search using ajax jquery therefore depending on the value typed into a textfield another php script runs that returns an array of rows in the database which is echoed and encoded with json using echo encode_json and this array is iterated in the javascript after the callback.

Why am I doing this?

  • I want to reuse the sql query where it searches for the matching rows in the database.
  • The livesearch will only display 5 matching results to the input data in the textbox and an extra "more option" tab. Theey will all be clickable eventually navigating to links within a web site but the more option tab will use the same sql query I used for the live search as it will direct into a page where it will display all the matching results.

This is my JavaScript could anyone assist me?

<script type="text/javascript">

function find(value){
    $( "#test" ).empty();
    var myExp = new RegExp(value , "i");
    $.ajax({
    url: 'searchDb.php',
    type: 'POST',
    data: 'args='+value+'&limit=2',
    success: function(data){
    var output = '<ul class="searchresults">';
    var count = 0;
    $.each(data, function(key, val) {
        if ((val.name.search(myExp) != -1)){
            output += '<li>';
            output += '<p>'+ val.name +'</p>';
            output += '</li>';
            count += 1;
            if(count == 5){
                output += '<li>';
                output += '<a href="searchResults.php">More Results</a>';
                output += '</li>';
                if(value.length == 0){             
                    output = '<ul class="searchresults">';
                }
                return false;
            }
        }
        if(value.length == 0){
            output = '<ul class="searchresults">';
        }
    });
    output += '</ul>';
    $('#test').html(output);

    }

});
}

Currently when I test this it does not work and the Google Chrome console shows an error

Uncaught TypeError: Cannot use 'in' operator to search for '22' in

["sss","sss"]

I have limit the data return to 2 just for test. It clearly it's returning it but I do not understanding cannot use 'in' operator to ........ Any help will be much appreciated! Thanks

PHP CODE:

<?php

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Not connected : ' . mysql_error());
 }


$db_selected = mysql_select_db('test', $link);
if (!$db_selected) {
die ('Can\'t use test : ' . mysql_error());
}
?>


<?php

function searchDb($abc, $limit = "all"){
$lists = array();
if (isset($abc)) {

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


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

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


while ( $row = mysql_fetch_assoc($result))
{

$var = $row["testa"];
array_push($lists, $var);
}
}


echo json_encode($lists);
}

$abc = $_POST['args'];
$limit = $_POST['limit'];
$lala = searchDb($abc, $limit);


?>
share|improve this question
    
post the part of php code where you build the responsed array of data –  Rami.Q Jan 16 '14 at 13:23
    
@Rami.Q thanks for the reply I just did it... –  user3104737 Jan 16 '14 at 13:30

1 Answer 1

you build your response Data Array this way:

while ( $row = mysql_fetch_assoc($result))
{

$var = $row["testa"];
array_push($lists, $var);
}

try to do it this way:

$RESPONSE = array();
$NAMES = array();

while ( $row = mysql_fetch_assoc($result))
    {

        $var = $row["testa"];
        array_push($NAMES,$var);
    }
$RESPONSE["NAMES"] = $NAMES;
print json_encode($RESPONSE);
exit();

in JS AJAX:

$.each(data.NAMES, function(key, val) {
        if ((val.search(myExp) != -1)){
.....

UPDATE: as requested in your comment!

the parameter of your function find(value) is undefined

so add the following at the top of your function

function find(value){

if (typeof foo == 'undefined')
return;
share|improve this answer
    
Thank you very much!!!!!!!!! –  user3104737 Jan 16 '14 at 15:19
    
Hello there @Rami.Q I have one problem as it doesn't recgonise .length now....Uncaught TypeError: Cannot read property 'length' of undefined –  user3104737 Jan 16 '14 at 15:35
    
length of what exactly? the data array length? if yes, then do data.NAMES.length. –  Rami.Q Jan 16 '14 at 16:22
    
@user3104737, if my answer above solved your question, then mark my answer as accepted Answer please –  Rami.Q Jan 16 '14 at 16:23
    
if(value.length == 0)....the value.length i guess? –  user3104737 Jan 16 '14 at 16:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.