根据从数据库中选择的值搜索数据库 [英] search the database based on the value selected from database

查看:111
本文介绍了根据从数据库中选择的值搜索数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



有两个页面的级联drop下拉列表。首先是index.php,其次是fetch_state.php。那里的代码是



index.php

 < body> 
< div id =container>
< div id =body>
< div class =mhead>< h2>使用jQuery Ajax和PHP的级联下拉列表 - InfoTuts< / h2>< / div>
< form class =form-horizo​​ntalrole =formaction =enctype =multipart / form-datamethod =post>
< div id =dropdowns>
< div id =centerclass =cascade>
<?php
$ sql =SELECT * FROM search_parent ORDER BY searchname;
$ query = mysqli_query($ con,$ sql);
?>
< label>国家/地区:
< select name =countryid =drop1>
< option value =>请选择< / option>
<?php while($ rs = mysqli_fetch_array($ query,MYSQLI_ASSOC)){?>
< option value =<?php echo $ rs [id];?>><?php echo $ rs [searchname]; ?>< /选项>
<?php}?>
< / select>
< / label>
< / div>

< div class =cascadeid =state>< / div>
< div id =cityclass =cascade>< / div>
< / div>

< div class =col-md-4 col-sm-6>
< div class =media-body>
< div class =col-md-8>
< input class =btn btn-primaryvalue =Searchtype =submitname =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# ();
// 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.gifalt =width =24height =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>状态:
< select name =stateid =drop2>
< option value =>请选择< / option>
<?php while($ rs = mysqli_fetch_array($ query,MYSQLI_ASSOC)){?>
< option value =<?php echo $ rs [id];?>><?php echo $ rs [fname]; ?>< /选项>
<?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];
/ /想从表格中获取所有数据
}
} else {
echo0 results;
}
mysqli_close($ con);
?>

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

解决方案

你的HTML代码添加

 < div class =showsearch> < / div> 

js代码

  $(document).ready(function(){
$('#drop2')。on变化,函数(){
变种FNAME = $(本).VAL();
//重命名您的文件,其中包含$ fname与get_search_data.php
if(fname!==){
$ .post('get_search_data.php',{fname:fname} ,function(data){
$('。showsearch')。html(data);
});
}
});
});




  • 该代码后应显示$ row ['fname'] to showsearch div

  • 你必须包含(connection.php);在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 ]; //这个数据应该出现在showsearch div
}
} else {
echo0 results;
}
mysqli_close($ con);
?>


I have a code for cascade drop down list that works perfectly fine, there are two drop down list and the 2nd one depends on the first one, however i wish to perform a search (from database table )based on the parameter that is choosed from the second drop down list. i have the code for search also, but i don't know how to combine the search code with the cascade drop down list code

There are two pages for the cascade drop down list. First is index.php and second is fetch_state.php. there code is

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>

the code above helps in my cascade list now the code for search that i wish to perform

<?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);
?>

As i mentioned earlier also, when the user selects the value from the second dropdown list i wish to get the search result based on the parameter that is selected in the 2nd drop down list and the result should be displayed on the same page below the search bar

解决方案

after your html code add

<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);
            });
          }
    });
});

  • after that code it should display the $row['fname'] to showsearch div
  • you have to include("connection.php"); in 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);
?>

这篇关于根据从数据库中选择的值搜索数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆