如何在PHP中向数据库提交依赖的dropdwon值 [英] How to submit dependant dropdwon values to database in PHP

查看:68
本文介绍了如何在PHP中向数据库提交依赖的dropdwon值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用数据库值创建了一个从属下拉列表。现在我想向搜索查询提交用户选择的两个选项,这将根据所选选项显示结果。在我试过的一整天,但我不能这样做是因为用户选择了两个值而没有传递给search.php文件。非常感谢帮助



我尝试过的事情:



这是获取数据库选项值并发送到search.php进行搜索的表格

I have created a dependent drop down list using database values.Now I want to submit user selected two options to a search query which will shows the result according the selected option.during the whole day I tried,But I couldn't do it because the user selected two values do not pass to the search.php file. A help would be really appreciated

What I have tried:

This is form which fetch databse option values and send to search.php for searching

form action="search.php" method="post">
     
<div class="col-lg-2"> 
    <select  name="country_name" id="country_name"  class="country">
    <option >Select Country</option>
        <?php
        include('config.php');
        $sql = mysqli_query($con,"select * from district");
        while($row=mysqli_fetch_array($sql))
        {
        echo '<option value="'.$row['country_id'].' ,'.$row['country_name'].'">'.$row['country_name'].'</option>';
        } ?>
    </select>           
    
</div>  
<div class="col-lg-2">               

    <select  class="city" id="city_name" name="city_name">
    <option>Select City</option>
    </select>
</div>
<input class="form-control" type="submit" name="submit" value="Search">
<script type="text/javascript">
$(document).ready(function()
{
$(".country").change(function()
{
var country_id=$(this).val();
var post_id = 'id='+ country_id;

$.ajax
({
type: "POST",
url: "ajax.php",
data: post_id,
cache: false,
success: function(cities)
{
$(".city").html(cities);
} 
});

});
});





这是search.php,它将执行搜索查询



This is search.php, which will execute search query

<?php include('config.php');?>
<?php

$country_name=$_POST["country_name"];
$services=$_POST["services"];
$city_name=$_POST["city_name"];
$sql="select * from services where district like '%" . $country_name . "%' AND service like '%" . $services . "%' AND city like '%". $city_name ."%' ";



$result = $con->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["district"]. $row["service"]. $row["city"];
    }
} else {
    echo "0 results";
}
$con->close();
?>





这是ajax.php,它将获取相关值



This is ajax.php which will fetch dependant values

<?php

include('config.php');
if($_POST['id']){
	$id=$_POST['id'];
	if($id==0){
		echo "<option>Select City</option>";
		}else{
			$sql = mysqli_query($con,"SELECT * FROM `city` WHERE country_id='$id'");
			while($row = mysqli_fetch_array($sql)){
				echo '<option value="'.$row['city_id'].','.$row['city_name'].'">'.$row['city_name'].'</option>';
				}
			}
		}
?>

推荐答案

sql = mysqli_query(
sql = mysqli_query(


con, select * from district);
while(
con,"select * from district"); while(


row = mysqli_fetch_array(
row=mysqli_fetch_array(


这篇关于如何在PHP中向数据库提交依赖的dropdwon值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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