2011-06-09 83 views
0

我試圖擺脫MySQL表期運用PHP和jQuery的自動完成功能的名單,但我沒有得到任何result.could你幫PLZ自動完成jQuery和PHP的問題

這裏是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代碼

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 

回答

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'; 

} 
+0

現在感謝我收到此錯誤消息:錯誤的查詢:您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到在第1行'FROM學生WHERE名稱LIKE a%'附近使用的正確語法 – Mary 2011-06-09 11:51:33

0

我想你想的服務器函數返回HTML的列表,而不是一個單一的字符串,它是所有的學生姓名的串聯。您是否使用過像fiddler這樣的工具來檢查從服務器發送和返回的內容?

0

爲簡單起見,如果您可以使用$.getJSON會比使用.load函數更容易解析jQuery。

由於您使用post這是可以做到這樣使用jQuery:

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:

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

將$ _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'; 
} 
0

真的很簡單。 JQuery做了大部分的努力工作。問題是從PHP到JavaScript的數據。然後在這之後,你將數據傳遞到jQuery的自動完成功能

例如:

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

下面是一個CUSTOMER_LIST陣列放入JavaScript的數據陣列。在輸入字段上使用jquery選擇器,然後添加自動完成功能。

希望有幫助