合并输入框和选择框以创建搜索栏

时间:2016-04-19 01:48:52

标签: javascript php jquery mysql

我有一个用作搜索框的输入框,我通过查询SQL数据库来填充选择框。例如,我的数据库中有两个名为“Dummy Organization”和“Dummy 2”的组织。因此,当用户开始键入“du”时,选择框将填充这两个组织。我想要做的是组合这两个字段:输入框(充当搜索框)和选择框(显示结果)。我的研究表明selectize.js可能有用,但由于我是js的新手,我无法弄清楚如何。有人可以帮帮我吗?我的代码如下:

从数据库获取数据的js:

<script type="text/javascript">
function searchq(){
    var searchTxt = $("input[name='search']").val();
    $.post("search.php", {searchVal: searchTxt},function(output4){
        $("#output4").html(output4);
}
</script>

表格:

<form action="newbrand.php" method="post" id="form">
        <br>
        Brand Name: <input type="text" name="bname" required /><br><br>
        Search for an Organization: <input type="text" required  name="search" onkeyup="searchq()" id="output"><br><br>         
        Selected Organization:
        <select id="output4" name="taskOption"></select>
</form>

search.php文件

<?php
include 'db_connect.php';
$link = mysqli_connect($host, $username, $password, $db);

if(!link){
echo "DB Connection error";
}
$output = '' ;
$output2 = '' ;
if (isset($_POST['searchVal'])){
$searchq = $_POST['searchVal'];
//$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysqli_query($link, "SELECT * FROM `organisations_info` WHERE `Organisation_Name` LIKE  '%".$searchq."%' ")or die("Could not search!");

$count = mysqli_num_rows($query);
if($count == 0){
    $output = '<option>No results!</option>';
}else{
    while($row = mysqli_fetch_array($query)){
        $orgname = $row['Organisation_Name'];
        $orgid = $row['Organisation_Id'];
        $subs = $row['Subscription_Type'];
    ?>
    <option value="<?php echo $orgid; ?>"><?php echo $orgname; ?></option>
    <div><?php echo $subs; ?></div>
    <?php
    } // while
} // else
} // main if
?>

0 个答案:

没有答案