PDA

View Full Version : Jquery : 'length' is null or not an object


ravin
02-05-2010, 10:04 AM
Message: 'length' is null or not an object
Line: 16
Char: 5
Code: 0

This is very frustrating!! Ive tried any IF ELSE but nothing works.

This is a Jquery search engine code.

If I type a word that exists in the database if(data.length >= 0) its OK
But if the word does not exist the above line freaks out with a length error.

function lookup(inputString) {
if(inputString.length <2) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >= 0) {

$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup

function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}

How can i overcome this? I want the Jquery to reply "No results" when there is no data from the database.

abduraooft
02-05-2010, 10:15 AM
if(data && data.length >= 0) {

$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}

ravin
02-05-2010, 10:48 AM
Thanks, the error is gone, but the search page is empty, is it possible to add a text "No results found" ?

Load this $('#suggestions').show();
Load text here -> $('#autoSuggestionsList').html(data);

When there is no length?

here is the rpc.php

<?php

// PHP5 Implementation - uses MySQLi.
// mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
$db = new mysqli('localhost', 'user' ,'pass', 'db');

if(!$db) {
// Show error if we cannot connect.
echo 'ERROR: Could not connect to the database.';
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);

// Is the string length greater than 0?

if(strlen($queryString) >0) {
// Run the query: We use LIKE '$queryString%'
// The percentage sign is a wild-card, in my example of countries it works like this...
// $queryString = 'Uni';
// Returned data = 'United States, United Kindom';

// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT name FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10

$query = $db->query("SELECT name FROM table WHERE name LIKE '$queryString%' LIMIT 10");
if($query) {
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
while ($result = $query ->fetch_object()) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.

// YOU MUST CHANGE: $result->value to $result->your_colum
echo '<li onClick="fill(\''.$result->name.'\');">'.$result->name.'</li>';
}
} else {
echo 'ERROR: There was a problem with the query.';
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo 'There should be no direct access to this script!';
}
}
?>

ravin
02-05-2010, 10:52 AM
Nevermind i resolved it myself, thanks for all help!


} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data && data.length > 0) {

$('#suggestions').show();
$('#autoSuggestionsList').html(data);
} else {$('#suggestions').show();
$('#autoSuggestionsList').html("No Results"); }