Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to to do autocomplete suggestion for my website and the data should be retrieved from database. I want to use JQuery autocomplete. here is my code but it doesn't work! This is my php file with the name of gethint.php:

<?php
require_once ('config.php');
$q=$_REQUEST["q"]; 
$sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
$result = mysql_query($sql);
$json=array();

while($row = mysql_fetch_array($result)) {
  $json[]=array(
  'value'=> $row['fname'],
  'label'=> $row['fname']
   );
   }
   echo json_encode($json);
  ?>

and then this is my html file :

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
 <script type="text/javascript"> 
 $(document).ready(function(){                  
 $("#hint").autocomplete({                        
 source:'gethint.php', 
 minLength:1                  
   }); 
   });        
</script>
</head>
<body>
<form class="sansserif" action="view.php" method="post">
Name: <input type="text" id="hint" name="hint" >
<input type="submit" name="submit" value="View">
</form>
</html>

It took a lot of time but I couldn't find the problem. I was wondering if someone could help me. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
398 views
Welcome To Ask or Share your Answers For Others

1 Answer

<?php
 require_once ('..config.php');
 $q=$_REQUEST["term"]; 
 $sql="SELECT `fname` FROM `Property` WHERE fname LIKE '%$q%'";
 $result = mysql_query($sql);

 while($row = mysql_fetch_array($result)) {
  $json[]=array(
   'value'=> $row['fname'],
   'label'=> $row['fname']
  );
 }
 echo json_encode($json);
?>

All I changed was the $_REQUEST["q"] to $_REQUEST["term"].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...