gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:06:15 25 4
gpt4 key购买 nike

我有一个cascade下拉列表的代码,工作得很好,有两个下拉列表,第二个下拉列表取决于第一个下拉列表,但是我希望根据从第二个下拉列表中选择的参数执行搜索(从数据库表)。我也有搜索代码,但我不知道如何将搜索代码与级联下拉列表代码结合起来
“层叠”下拉列表有两页。第一个是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>

获取状态.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);
?>

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

最佳答案

添加html代码后

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

js代码
$(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['fname']以显示search div
您必须在get_search_data.php中包含(“connection.php”)
获取搜索数据.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);
?>

关于php - 根据从数据库中选择的值搜索数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27329035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com