Here i am selecting company name as an option
Here i need to show relevant categories under company name;
How can i do this?
You can do that by using Jquery and Ajax. This is a simple code snippet to achieve what you want.
First, inside your index.php:
<script
type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".company").change(function(){
var company = $(this).val();
var dataString = 'company=' + company;
$.ajax({
type: "POST",
url: "business.php",
// You can edit choice2.php for other filename you want
data: dataString,
cache: false,
success: function(html){
$(".business").html(html);
}
});
});
});
</script>
Company :
<select name="company" class="company">
<option selected="selected">--Select Company--</option>
<?php
include('db.php');
$sql= "SELECT Company FROM table_test";
// You should edit table_test for your table name
$result = mysqli_query($con ,$sql);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$company = $row['Company'];
echo '<option value="'.$company.'">'.$company.'</option>';
}
?>
</select>
<br/><br/>
Business :
<select name="business" class="business">
<option selected="selected">--Select Business--</option>
</select>
Then inside your second file. I called it business.php:
<?php
include('db.php');
if($_POST['company']){
$company = $_POST['company'];
$sql= "SELECT Business FROM table_test WHERE Company='$company'";
$result = mysqli_query($con ,$sql);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$business = $row['Business'];
echo '<option value="'.$business.'">'.$business.'</option>';
}
}
?>
Then to connect to the database you can use this code snippet. I called this file db.php.
<?php
$con = mysqli_connect("localhost","root","","database_test");
// You should edit database_test with your database name
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
?>