Customer textfield with autocomplete from databasePHP&jQueryのは -
私が入力中のテキストで開始し、顧客を表示するオートコンプリートを持つ1つの顧客テキストフィールドを作成することに成功したオートコンプリートは、データベースから取得したデータの異なるリストを持つ二つの異なるテキストフィールドを作成します。 1つのテキストフィールド
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$("#customer").autocomplete({
source: "../phpfiles/search.php",
});
});
</script>
<div class="ui-widget">
<!-- action='./../customer_report_request' -->
<form id="customer_report_request" name="customer_report_request" method="post">
<table>
<tr>
<th colspan='2'>Search Customer</th>
</tr>
<tr>
<td>
<label>Customer: </label>
<input name="customer" id="customer" value='' required>
</td>
<td>
<label>Submit: </label>
<input value="Send" name="send_customer_request" type="submit" id="send_customer_request">
</td>
</tr>
</table>
</form>
</div>
<?php
//Display the list of customer details
if(isset($_POST['send_customer_request']))
{
include 'db.php'; //connection
$query = "SELECT * FROM customer WHERE Company_Name = '".$_POST['customer']."'";
$customer_result = $db->query($query);
$count_customer = $customer_result->num_rows;
if($count_customer>0)
{
echo"<div>";
echo "<table>";
echo"<tr>";
echo"<th>Company_Name</th>";
echo"<th>VAT_Registration</th>";
echo"<th>Contact_First_Name</th>";
echo"<th>Contact_Last_Name</th>";
echo"<th>Email</th>";
echo"</tr>";
while ($row = $customer_result->fetch_assoc())
{
echo"<tr>";
echo"<td>".$row['Company_Name']."</td>";
echo"<td>".$row['VAT_Registration']."</td>";
echo"<td>".$row['Contact_First_Name']."</td>";
echo"<td>".$row['Contact_Last_Name']."</td>";
echo"<td>".$row['Email']."</td>";
echo"</tr>";
}
echo "</table>";
echo"</div>";
}
$db->close();
}
?>
ため
のindex.php Search.php 1つのテキストフィールド
<?php
$dbHost = 'localhost';
$dbUsername = 'bobo';
$dbPassword = 'rodnik';
$dbName = 'training';
//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term
$searchTerm = $_GET['term'];
//get matched data from customer table
$query = $db->query("SELECT * FROM customer WHERE Company_Name LIKE '".$searchTerm."%' ORDER BY Company_Name ASC"); //Starts with
while ($row = $query->fetch_assoc()) {
$data[] = $row['Company_Name'];
}
//return json data
echo json_encode($data);
?>
のための問題は、私は他のに対応するために単一の検索PHPファイルを使用したいですクエリ。たとえば :ワードが連絡先のテキストフィールドに入力された場合
- 、クエリは、クエリ、単語が顧客テキストフィールドに入力された場合
- 「....コンタクトSELECT * FROM」 になります "SELECT * FROM Customer ...."
これを達成するためにindex.phpとsearch.phpの両方が変更されました。
index.phpの中の修飾部分
jQueryの変数は、COMPONENT_NAMEを定義しました。 index.phpファイルから、お客様のtexfieldは、search.phpファイルに変数を送信し、POSTメソッドを使用して、その変数を識別して照会の目的で使用できるようにします。
連絡先テキストフィールドは、index.phpファイルまたは別のPHPファイル内の同じ形式のいずれかにすることができます。
<script>
$(function() {
$("#customer").autocomplete({
var component_name= "customer";
source: "../phpfiles/search.php",
minLength: 1,
change: function(event, ui)
{
$.post("../phpfiles/search.php", data{post_data: component_name});
}
});
});
</script>
修正search.php
<?php
$dbHost = 'localhost';
$dbUsername = 'bobo';
$dbPassword = 'rodnik';
$dbName = 'training';
//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term
$searchTerm = $_GET['term'];
//get matched data from skills table
$query="";
if($_POST['post_data']=="customer")
{
$query = $db->query("SELECT * FROM customer WHERE Company_Name LIKE '".$searchTerm."%' ORDER BY Company_Name ASC"); //Starts with
while ($row = $query->fetch_assoc())
{
$data[] = $row['Company_Name'];
}
//return json data
echo json_encode($data);
}
?>
誰もがこれを達成するために私を助けることができますか?
私はjqueryの-UIとjQuery APIの部品のため、これらのリンクを使用:これは私が願って少し複雑広告かもしれ
- api.jquery.com
- jqueryui.com
ようこそスタックオーバーフロー。 index.phpで検索結果を編集するときにこれを使用していますか?あなたがそれを提示したやり方を少し混乱させます。私は両方のテキストフィールドを選択するためにクラスセレクタを使用します。そして、あなたの 'ソース'では、どのフィールドが使用されているのかを判断し、それに基づいて検索を実行できます。 – Twisty