Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
HTML
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);
} 
});

});
});


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


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>';
				}
			}
		}
?>
Posted
Updated 24-Jul-18 2:57am

1 solution

You didn't say if any of your values are getting to search.php.

You also don't show us what/where you go to load your OPTION elements for SELECT. Because of this, your probably could be as simple as you not setting your value="" in each option so you get no values.

For that matter, your SQL search is using LIKE clauses - but if you're picking values from a SELECT list they're exact values and they should be "=" not "LIKE" because you specified them (not the user).

Check the above - do you actually have any values at all for your options. Here's what I mean:

HTML
<option value="this is a value">This is NOT a value</option>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900