2016-01-07 47 views
0

我有两个文本框,我希望它们从mysql database的两个不同列自动完成,我得到第一个结果,但是当我使用相同的代码对于第二个,它不工作。 我打电话我就onkeyupautocomplet()在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相同的代码不工作!

+0

当我为staff_name编写相同的代码并将代码中的客户端名称替换为员工姓名时,它不起作用.i只是想知道我是否也可以为员工使用数据{keyword:keyword}?在我的代码中,冒号前后的关键字代表什么? –

+0

我会使用类而不是ids'$('#clientname').val(item);'。 – Huelfe

+0

会不会做任何差异? –

回答

0

我会这么做:

jQuery的

function autocomplete_client() { 
    var min_length = 0; // min caracters to display the autocomplete 
    var keyword = $('#clientname').val(); 
    if (keyword.length >= min_length) { 
     $.ajax({ 
      url: 'ajax_refresh_client.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_client(item) { 
    // change input value 
    $('#clientname').val(item); 
    // hide proposition list 
    $('#clientlist_id').hide(); 
} 

function autocomplete_staff() { 
    var min_length = 0; // min caracters to display the autocomplete 
    var keyword = $('#staff').val(); 
    if (keyword.length >= min_length) { 
     $.ajax({ 
      url: 'ajax_refresh_staff.php', 
      type: 'POST', 
      data: {keyword:keyword}, 
      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_item_staff(item) { 
    // change input value 
    $('#staff').val(item); 
    // hide proposition list 
    $('#stafflist_id').hide(); 
} 

ajax_refresh_client.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_client(\''.str_replace("'", "\'", $rs['client_name']).'\')">'.$client_name.'</li>'; 
} 

ajax_refresh_staff.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(); 
$keyword = '%'.$_POST['keyword'].'%'; 
$sql = "SELECT * FROM newdata WHERE staff 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 
    $staff = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['staff']); 
    // add new option 
    echo '<li onclick="set_item_staff(\''.str_replace("'", "\'", $rs['staff']).'\')">'.$staff.'</li>'; 
} 

HTML

<div class="input_container"> 
    <label for="Clientname">Client Name</label></br> 
    <input type="text" name="clientname" id="clientname" onkeyup="autocomplete_client()" /> 
    <ul id="clientlist_id"></ul> 
</div> 
<div class="input_container"> 
    <label for="Staff">Staff</label></br> 
    <input type="text" name="staff" id="staff" onkeyup="autocomplete_staff()" /> 
    <ul id="stafflist_id"></ul> 
</div> 

确保清除缓存。这段代码可能更干净,但它可以工作。

+0

它的工作非常感谢alt ..但是我的代码有什么问题?反正谢谢@Huelfe –

+0

如上所述。你的html无效。 ;) – Huelfe

相关问题