Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry Jquery : 'length' is null or not an object

    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.

    Code:
    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.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Code:
    if(data && data.length >= 0) {
    				
    	$('#suggestions').show();
    	$('#autoSuggestionsList').html(data);
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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!';
            }
        }
    ?>
    Last edited by ravin; 02-05-2010 at 12:52 PM.

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nevermind i resolved it myself, thanks for all help!

    PHP Code:
    } 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"); } 
    Last edited by ravin; 02-05-2010 at 04:25 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •