2017-09-05 29 views
0

我在使用PHP在Wordpress中填充一個Sql數組時出現問題。Jquery SQL Array Wordpress

我search.html

<form action="" method="post"> 
<input type="text" placeholder="Name" id="customerAutocomplte" class="ui-autocomplete-input" autocomplete="on" /> 
</form> 

<script src="js/jquery.ui.autocomplete.html.js" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
<script> 
$.noConflict(); 
$(document).ready(function($){ 
    $('#customerAutocomplte').autocomplete({ 
    source:'suggest_name.php', 
    minLength:2 
    }); 
}); 
</script> 
</head> 

,這裏是suggest_name.php

/* Attempt MySQL server connection */ 
$link = mysqli_connect("localhost", "root", "mysql", "wordpress"); 

// Check connection 
if($link === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 
else { 
    $link->set_charset('utf8'); 
} 
/* retrieve the search term that autocomplete sends */ 
$term = trim(strip_tags($_GET['term'])); 
$a_json = array(); 
$a_json_row = array(); 
if ($data = $link->query("SELECT * FROM persons WHERE first_name LIKE '%$term%' OR last_name LIKE '%$term%' ORDER BY first_name , last_name")) { 
    while($row = mysqli_fetch_array($data)) { 
     $first_name = htmlentities(stripslashes($row['first_name '])); 
     $last_name = htmlentities(stripslashes($row['last_name'])); 
     $code = htmlentities(stripslashes($row['customer_code'])); 
     $a_json_row["id"] = $code; 
     $a_json_row["value"] = $first_name.' '.$last_name; 
     $a_json_row["label"] = $first_name.' '.$last_name; 
     array_push($a_json, $a_json_row); 
    } 
} 
// jQuery wants JSON data 
echo json_encode($a_json); 
flush(); 

我的SQL表

「人」

有4列

「FIRST_NAME」, 「姓氏」, 「電子郵件」 和 「customer_code」

「Customer_code」 是 「AI」 主鍵。我的問題是,搜索不會自動填充或填充數組,沒關係,sql數據庫包含條目,謝謝。

回答

0

根據documentation您或者需要指定一個appendTo選擇器,或者輸入需要與ui-front類有兄弟關係。

the same documentation,你source可以是一個ArrayStringFunction(Object request, Function response(Object data))。您正在使用PHP文件,這兩者都不是。你必須編寫一個函數,它可以發送和接收來自php文件的信息,以使其工作。 AJAX可能是這種腳本的最佳選擇,因爲它允許異步通信(即不需要重新加載頁面來與PHP「交談」)。

這兩個問題本身就足以破壞你的代碼。我可能錯過了其他的東西,但我建議先解決這兩件事。