我有兩個文本框,我希望它們從mysql database
的兩個不同列自動完成,我得到第一個結果,但是當我使用相同的代碼對於第二個,它不工作。 我打電話我就onkeyup
autocomplet()
在HTML
的jQuery:
function autocomplet() {
var min_length = 0; // min caracters to display the autocomplete
var keyword = $('#clientname').val();
if (keyword.length >= min_length) {
$.ajax({
url: 'ajax_refresh.php',
type: 'POST',
data: {keyword:keyword},
success:function(data){
$('#clientlist_id').show();
$('#clientlist_id').html(data);
}
});
} else {
$('#clientlist_id').hide();
}
}
// set_item : this function will be executed when we select an item
function set_item(item) {
// change input value
$('#clientname').val(item);
// hide proposition list
$('#clientlist_id').hide();
}
function autocomplete() {
var min_lengthstaff = 0; // min caracters to display the autocomplete
var input = $('#staff').val();
if (input.length >= min_lengthstaff) {
$.ajax({
url: 'autocompletephpcode.php',
type: 'POST',
data: {input:input},
success:function(data){
$('#stafflist_id').show();
$('#stafflist_id').html(data);
}
});
} else {
$('#stafflist_id').hide();
}
}
// set_item : this function will be executed when we select an item
function set_itemstaff(items) {
// change input value
$('#staff').val(items);
// hide proposition list
$('#stafflist_id').hide();
}
ajax_refresh.php:
<?php
// PDO connect *********
function connect() {
return new PDO('mysql:host=localhost;dbname=entry', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
//for client name
$pdo = connect();
$keyword = '%'.$_POST['keyword'].'%';
$sql = "SELECT * FROM newdata WHERE client_name LIKE (:keyword) ORDER BY id ASC LIMIT 0, 10";
$query = $pdo->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
// put in bold the written text
$client_name = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['client_name']);
// add new option
echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['client_name']).'\')">'.$client_name.'</li>';
}
?>
autocompletephpcode.php:
<?php
// PDO connect *********
function connect() {
return new PDO('mysql:host=localhost;dbname=entry', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
//for staff name
$pdo = connect();
$input = '%'.$_POST['input'].'%';
$sql = "SELECT * FROM newdata WHERE staff LIKE (:input) ORDER BY id ASC LIMIT 0, 10";
$query = $pdo->prepare($sql);
$query->bindParam(':input', $input, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
// put in bold the written text
$staff = str_replace($_POST['input'], '<b>'.$_POST['input'].'</b>', $rs['staff']);
// add new option
echo '<li onclick="set_itemstaff(\''.str_replace("'", "\'", $rs['staff']).'\')">'.$staff.'</li>';
}
?>
這是我的HTML的
<form id="dataentry" action="ajax_refresh.php" method="post" role="form" >
<div class="input_container">
<label for="Clientname">Client Name</label></br>
<input type="text" name="clientname" id="clientname" onkeyup="autocomplet()" />
<ul id="clientlist_id"></ul>
</div>
</form>
<form action="autocompletephpcode.php" method="post" role="form">
</div>
<div class="form-group col-md-2">
<div class="input_container">
<label for="Staff">Staff</label></br>
<input type="text" name="staff" id="staff" onkeyup="autocomplete()" />
<ul id="stafflist_id"></ul>
</div>
爲client_name
相同的代碼不工作!
當我爲staff_name編寫相同的代碼並將代碼中的客戶端名稱替換爲員工姓名時,它不起作用.i只是想知道我是否也可以爲員工使用數據{keyword:keyword}?在我的代碼中,冒號前後的關鍵字代表什麼? –
我會使用類而不是ids'$('#clientname').val(item);'。 – Huelfe
會不會做任何差異? –