Friday 17 February 2017

Dynamic Drop Down

(1)create  table   mystate  :

create table  mysate
(id  int primary key auto_increment,
state  varchar(200)
);

(2)create table  mycity :

Create table  mycity
(id  int primary key auto_incrment,
city   varchar(200),
state  varchar(200)
);

(3)insert    some values   in mystate  & mycity ..

(4)Now   write code   for   db.php    file:

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("ajax",$con);
?>
(5)Now  write code for  drop.php  file:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sections Demo</title>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<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()
{
$(".country").change(function()
{
var data=$(this).val();
var dataString = 'data='+ data;

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

});
});
</script>
<style>
label
{
font-weight:bold;
padding:10px;
}
</style>
</head>
<body>
<form action=test.php    method=post  enctype="multipart/form-data">
<div style="margin:80px">
<label>Country :</label>
<select name="country" class="country">
<option selected="selected">--Select Country--</option>

<?php
include('db.php');
echo $sql1="select * from  mystate";
$result1=mysql_query($sql1) or die(mysql_error());
while($row1=mysql_fetch_array($result1))
{
              
                echo '<option value="'.$row1['state'].'">';
                echo  $row1['state'];
                echo '</option>';
              
}
?>
</select>
<input type=submit name=submit value=submit>
</form> <br/><br/>
<label>City :</label>
<div name="city" class="city">
</div>
</div>
</body>
</html>

(6)write   code for  ajax_city.php file:

<select name="categoryname" id="categoryname">';
<?php
include("db.php");
$type=$_POST['data'];
echo $sql2="select * from mycity where state='$type'";
$result2=mysql_query($sql2) or die(mysql_error());
while($row2=mysql_fetch_array($result2))
{
                echo '<option value="'.$row2['city'].'">';
                echo  $row2['city'];
                echo '</option>';
}

?>
</select>







1 comment:

  1. It’s very informative and you are obviously very knowledgeable in this area. You have opened my eyes to varying views on this topic with interesting and solid content. סיינטולוגיה

    ReplyDelete