自动完成jQuery& php问题

时间:2011-06-09 11:32:23

标签: php jquery

我正在尝试使用php和jQuery自动完成功能从MySQL表中获取名称列表,但我得不到结果。你能帮助plz

这里是jQuery

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" 
  type="text/javascript"></script> 

<script language="javascript" type="text/javascript">
  function findmatch(str){
    if (str.length>0){
      $('#found').load('SelectQury.php?n='+str);
    }
  }//fun
</script>
</head>

<body>
  <div id="container">
    <input type="text" name="text" id="textfield" autocomplete="off"    
     onkeyup="findmatch(this.value);"/>
    <div id="found" >non found</div>
  </div>

</body>
</html>

php code

if(isset($_POST['text'])){
    $s=$_POST['text'];

$query="SELECT FROM student WHERE name LIKE '$s%'";
$result=mysql_query($query) or die ('Wrong query : ' . mysql_error() );
$string='';
while($row = mysql_fetch_assoc($result)){
     $string.= $row['name'];
}
echo $string;
}else{
  echo 'wrong GET keywoord';

}//ifisset

4 个答案:

答案 0 :(得分:0)

两个问题:

1. you are sending the value as "GET" method `?n=` and accessing in the PHP code as `$_POST`
2. the passed variable is `n` and not `text`
3. error in mysql query

使用以下PHP代码:

if(isset($_GETT['n'])){
   $s=$_GET['n'];

    $query="SELECT * FROM student WHERE name LIKE '$s%'";
    $result=mysql_query($query) or die ('Wrong query : ' . mysql_error() );
    $string='';
    while($row = mysql_fetch_assoc($result)){
         $string.= $row['name'];
    }
      echo $string;
  }else{
     echo 'wrong GET keywoord';

}

答案 1 :(得分:0)

我认为您希望服务器函数返回HTML列表,而不是单个字符串,它是所有学生名称的串联。您是否使用了像fiddler这样的工具来检查从服务器发送和返回的内容?

答案 2 :(得分:0)

如果你可以使用$.getJSON来简化jQuery可以比使用.load函数快速解析那么会更容易。

由于您正在使用post,因此可以使用jQuery以这种方式完成:

var inputName = $('input[name=text]').val();

$.post('SelectQury.php', { text: inputName }, function(response)
{
    var nameBuild = '';
    for(i = 0; i < response.length; i++)
    {
        nameBuild += '<div class="row">' + response[i].name + '</div>';
    }

    $(nameBuild).appendTo('#found');
}, 'JSON');

如果您使用的是get,请执行以下操作$.getJSON

var inputName = $('input[name=text]').val();

$.getJSON('SelectQury.php', { name: inputName }, function(response)
{
    var nameBuild = '';
    for(i = 0; i < response.length; i++)
    {
        nameBuild += '<div class="row">' + response[i].name + '</div>';
    }

    $(nameBuild).appendTo('#found');
});

PHP:

如果你使用的是get而不是post

,请将$ _POST替换为$ _GET
if(isset($_POST['text']))
{
    $s = mysql_real_escape_string($_POST['text']);

    $query = "SELECT FROM student WHERE name LIKE '$s%'";
    $result = mysql_query($query) or die ('Wrong query : ' . mysql_error() );

    $string = array();
    while($row = mysql_fetch_assoc($result))
    {
         $string[] = array('name' => $row['name']);
    }

    echo json_encode($string);
}
else
{
    echo 'wrong GET keywoord';
}

答案 3 :(得分:0)

真的很简单。 JQuery完成了大部分的艰苦工作。问题是从php获取数据到javascript。然后,将数据传递给jquery的自动完成功能

示例:

var data = "<?php echo implode(",",$customer_list) ?>".split(",");
    $("#CUSTOMER_NAME" ).autocomplete({source: data});

这是一个放入javascript数据数组的customer_list数组。在输入字段上使用jquery选择器,然后添加自动完成功能。

希望有所帮助