根据从数据库中选择的值搜索数据库

时间:2014-12-06 06:34:44

标签: php mysql sql drop-down-menu mysqli

我有一个级联下拉列表的代码完全正常,有两个下拉列表,第二个依赖于第一个,但是我希望根据参数执行搜索(从数据库表)从第二个下拉列表中选择。我也有搜索代码,但我不知道如何将搜索代码与级联下拉列表代码结合起来

级联下拉列表有两个页面。首先是index.php,第二个是fetch_state.php。代码是

index.php

<body>
    <div id="container">
        <div id="body">
            <div class="mhead"><h2>Cascaded dropdown with jQuery Ajax and PHP - InfoTuts</h2></div>
                <form class="form-horizontal" role="form" action="" enctype="multipart/form-data" method="post">
                    <div id="dropdowns">
                        <div id="center" class="cascade">
                            <?php
                                $sql = "SELECT * FROM search_parent ORDER BY searchname";
                                $query = mysqli_query($con, $sql);
                            ?>
                            <label>Country:
                                <select name="country" id = "drop1">
                                    <option value="">Please Select</option>
                                        <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
                                    <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["searchname"]; ?></option>
                                    <?php } ?>
                                </select>
                            </label>
                        </div>

                        <div class="cascade" id="state"></div> 
                        <div id="city" class="cascade"></div> 
                    </div>

                    <div class="col-md-4 col-sm-6">
                        <div class="media-body">
                            <div class="col-md-8">
                                <input class="btn btn-primary" value="Search" type="submit" name="submit">
                            </div>
                        </div>
                    </div>
                </form>         
        </div>
    </div>

    <script src="jquery-1.11.1.js"></script>
    <script>
    $(document).ready(function()
        {
            $("select#drop1").change(function(){
                var parent_id =  $("select#drop1 option:selected").attr('value'); 
                // alert(parent_id);    
                $("#state").html( "" );
                //$("#city").html( "" );
                if (parent_id.length > 0 ){ 
                    $.ajax({
                        type: "POST",
                        url: "fetch_state.php",
                        data: "parent_id="+parent_id,
                        cache: false,
                        beforeSend: function (){ 
                                $('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
                            },
                        success: function(html){    
                                $("#state").html( html );
                            }
                        });
                    } 
                });
        });
    </script>
</body>

fetch_state.php

<?php
include("connection.php");
$parent_id = trim(mysql_escape_string($_POST["parent_id"]));

$sql = "SELECT * FROM features WHERE parent_id = ".$parent_id ." ORDER BY fname";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>State: 
<select name="state" id="drop2">
    <option value="">Please Select</option>
    <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
    <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["fname"]; ?></option>
    <?php } ?>
</select>
</label>
<?php 
    }
?>
<script src="jquery-1.11.1.js"></script>

上面的代码现在帮助我的级联列表中我想要执行的搜索代码

<?php
$fname = mysqli_real_escape_string($con, $_POST['fname']);

$sql1 = "SELECT * FROM office WHERE fname LIKE '%$fname%';
$result = mysqli_query($con, $sql1);

if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) {
        echo  $row["fname"];
        //would like to get all the data from the table here
    }
} else {
    echo "0 results";
}
mysqli_close($con);
?>

正如我之前提到的,当用户从第二个下拉列表中选择值时,我希望根据在第二个下拉列表中选择的参数获得搜索结果,结果应显示在同一页面上在搜索栏下方

1 个答案:

答案 0 :(得分:1)

在你的html代码添加

之后
<div class="showsearch"></div>

js code

$(document).ready(function(){
    $('#drop2').on('change',function(){
         var fname = $(this).val();
         // rename your file which include $fname  with get_search_data.php
         if(fname !== ""){
            $.post('get_search_data.php',{fname: fname},function(data){
            $('.showsearch').html(data);
            });
          }
    });
});
  • 在该代码之后,它应该显示$ row [&#39; fname&#39;]到showsearch div
  • 你必须包括(&#34; connection.php&#34;);在get_search_data.php

get_search_data.php

<?php
include("connection.php");
if(isset($_POST['fname'])){
$fname = mysqli_real_escape_string($con, $_POST['fname']);
}
$sql1 = 'SELECT * FROM office WHERE fname LIKE "%'.$fname.'%"';
$result = mysqli_query($con, $sql1);

if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) {
        echo  $row["fname"]; // this data should appear in showsearch div
    }
} else {
    echo "0 results";
}
mysqli_close($con);
?>