jQuery autocomplete: undefined index term (2)

3 replies
This is the continuous discussion post that I should post in the reply of my other post here:
http://www.warriorforum.com/programm...ndex-term.html
But, I tried hard to post their and was unsuccessful, and I have already discussed the reason of this problem here: http://www.warriorforum.com/suggesti...g-replies.html
But I didn't get any positive response. That's why I am posting in a new thread now.

OK. I have solved the problem of Undefinex index that I mentioned in the last post by adding the condition "if ( !isset($_REQUEST['term']) )
" but now there is another problem. It doesn't show me the suggestions. Whenever I enter 2 digits of zip code, the scrip shows me the message "No search results." However, the data exists in database. Below is the screenshot of the SAMPLE database table that I have created for this test work.



And here is the link where I am testing this script where you can see that it doesn't work.
Suggest Zip

And here is the code of full page.

Code:
<html>
<head>

<title>Suggest Zip</title>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script>
jQuery(document).ready(function($){
	$('#zipsearch').autocomplete({source:'suggest_zip.php', minLength:2});
});
</script>
</head>

<body>

<form action="" method="post">
	Enter your zipcode:
	<input type="text" id="zipsearch" />
 
	<br />
	<input type="submit" value="Search" />
</form>

<?php

// if the 'term' variable is not sent with the request, exit
if ( !isset($_REQUEST['term']) )
	exit;

// connect to the database server and select the appropriate database for use
$dblink = mysqli_connect('mysql5.000webhost.com', 'a8995753_db2	', 'xxxxxxxxx', 'a8995753_db2') or die("Error: " . mysqli_connect_error() );

// query the database table for zip codes that match 'term'
$rs = mysqli_query($dblink, 'select zipCode, city, state from zipcode where zipCode like "'. mysqli_real_escape_string($dblink, $_REQUEST['term']) .'%" order by zipCode asc limit 0,10') or die( "Error: " . mysqli_error($dblink) );

// loop through each zipcode returned and format the response for jQuery
$data = array();
if ( $rs && mysqli_num_rows($rs) )
{
	while( $row = mysqli_fetch_array($rs, MYSQL_ASSOC) )
	{
		$data[] = array(
			'label' => $row['zipCode'] .', '. $row['city'] .' '. $row['state'] ,
			'value' => $row['zipCode']
		);
	}
}

// jQuery wants JSON data
echo json_encode($data);
flush();
?>

</body>
</html>
#autocomplete #index #jquery #undefined
  • Profile picture of the author Brandon Tanner
    Where are you trying to request 'term' from? Another page? Or the same page? Databases are not my strong point, but if you are trying to request 'term' from the $rs line on the same page, then shouldn't this...

    if ( !isset($_REQUEST['term']) )
    exit;

    ... be after the $rs line?

    At any rate, what you can do is use !isset conditional statements after each of the PHP variables (term, dblink, rs, etc), and then use identifying text for each exit line, so that you can pinpoint exactly where the problem is. For example...

    if ( !isset($_REQUEST['term']) )
    exit("term is not set");

    $dblink = blah blah blah...
    if ( !isset($dblink) )
    exit("dblink is not set");

    etc. etc.
    Signature

    {{ DiscussionBoard.errors[8211572].message }}
    • Profile picture of the author freetutorial
      Pls check your Mysql Query

      $rs = mysqli_query($dblink, 'select zipCode, city, state from zipcode where zipCode like "%'. mysqli_real_escape_string($dblink, $_REQUEST['term']) .'%" order by zipCode asc limit 0,10') or die( "Error: " . mysqli_error($dblink) );


      Missing "%" in your mysql query. Should be Like '% search word %'
      {{ DiscussionBoard.errors[8260746].message }}
      • Profile picture of the author phpg
        First, i'd suggest you use some Javascript debugger like Firebug, you will be able to see ajax response (and errors there if any) using it. Your page shows this:
        Code:
        <b>Warning</b>:  mysqli_connect() [<a href='function.mysqli-connect'>function.mysqli-connect</a>]: (28000/1045): Access denied for user 'a8995753_db2	'@'10.1.1.22' (using password: YES) in <b>/home/a8995753/public_html/suggest_zip.php</b> on line <b>33</b><br />
        - it looks like you have some extra space in the database username here:

        $dblink = mysqli_connect('mysql5.000webhost.com', 'a8995753_db2 ', 'xxxxxxxxx', 'a8995753_db2')

        Second, you should put if ( !isset($_REQUEST['term']) ) at the top and the whole html block should be under this condition, otherwise it is called and outputs html always, even when only json output is expected. Closing </body></html> you should also put under this top block, not at the bottom of the page.
        {{ DiscussionBoard.errors[8261502].message }}

Trending Topics