我有两个文本框,我希望它们从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
会不会做任何差异? –