How to Create Dynamic Dependent Select Box

How to Create Dynamic Dependent select box using ajax

You need a dynamic dependent select box. We will explain how to create a dynamic dependent select box in PHP, jquery and ajax.

index.php

<?php
include_once 'dbconfig.php';
?>
<html>
<head>
<title>Dynamic Dependent Select Box using Ajax</title>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
	$("#loding1").hide();
	$("#loding2").hide();
	$(".country").change(function()
	{
		$("#loding1").show();
		var id=$(this).val();
		var dataString = 'id='+ id;
		$(".state").find('option').remove();
		$(".city").find('option').remove();
		$.ajax
		({
			type: "POST",
			url: "get_state.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#loding1").hide();
				$(".state").html(html);
			} 
		});
	});
	
	
	$(".state").change(function()
	{
		$("#loding2").show();
		var id=$(this).val();
		var dataString = 'id='+ id;
	
		$.ajax
		({
			type: "POST",
			url: "get_city.php",
			data: dataString,
			cache: false,
			success: function(html)
			{
				$("#loding2").hide();
				$(".city").html(html);
			} 
		});
	});
	
});
</script>
<style>
label
{
font-weight:bold;
padding:10px;
}
div
{
	margin-top:100px;
}
select
{
	width:200px;
	height:35px;
	border:2px solid #456879;
	border-radius:10px;
}

.color {
	color:green;
}

.link {
	color:red;
}
</style>
</head>

<body>
<h1 align="center" class="color"> Daynamic Dropdown in PHP &MySql Ajax And jQuery</h1>
<center>
<div>
<label>Country :</label> 
<select name="country" class="country">
<option selected="selected">--Select Country--</option>
<?php
	$stmt = $DB_con->prepare("SELECT * FROM country");
	$stmt->execute();
	while($row=$stmt->fetch(PDO::FETCH_ASSOC))
	{
		?>
        <option value="<?php echo $row['country_id']; ?>"><?php echo $row['country_name']; ?></option>
        <?php
	} 
?>
</select>
<br><br><br>
<label>State :</label> 
<select name="state" class="state">
<option selected="selected">--Select State--</option>
</select>
<img src="ajax-loader.gif" id="loding1"></img>
<br><br><br>
<label>City :</label> 
<select name="city" class="city">
<option selected="selected">--Select City--</option>
</select>
<img src="ajax-loader.gif" id="loding2"></img>
<br><br><br>
</div>
<br />
</center>
</body>
</html>

dbconfig.php

<?php

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "database";

try
{
	$DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
	$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
	$e->getMessage();
}

get_city.php

<?php
include('dbconfig.php');
if($_POST['id'])
{
	$id=$_POST['id'];
	
	$stmt = $DB_con->prepare("SELECT * FROM city WHERE state_id=:id");
	$stmt->execute(array(':id' => $id));
	?><option selected="selected">Select City :</option>
	<?php while($row=$stmt->fetch(PDO::FETCH_ASSOC))
	{
		?>
		<option value="<?php echo $row['city_id']; ?>"><?php echo $row['city_name']; ?></option>
		<?php
	}
}
?>

get_state.php

<?php
include('dbconfig.php');
if($_POST['id'])
{
	$id=$_POST['id'];
		
	$stmt = $DB_con->prepare("SELECT * FROM state WHERE country_id=:id");
	$stmt->execute(array(':id' => $id));
	?><option selected="selected">Select State :</option><?php
	while($row=$stmt->fetch(PDO::FETCH_ASSOC))
	{
		?>
        	<option value="<?php echo $row['state_id']; ?>"><?php echo $row['state_name']; ?></option>
        <?php
	}
}
?>

Check on Facebook

59 Comments

  • real life cam

    August 29, 2020 - 3:38 am

    Do you mind if I quote a few of your articles as long as I provide credit andd sources back
    to your weblog? My blog site iss in the very same area oof interest as yours annd my visitors would certainly benefit from a lot of the
    information youu provide here. Please let me know iff this okay
    with you. Cheers!

  • Souris Silencieuse

    August 29, 2020 - 3:16 am

    Excellent goods from you, man. I’ve have in mind your stuff previous to and you’re simply too fantastic.
    I really like what you’ve received here, certainly like what
    you’re saying and the best way in which you say it. You are making it entertaining and you still care for to
    stay it smart. I cant wait to learn much more
    from you. That is really a wonderful website.

  • zapacita01

    August 29, 2020 - 2:44 am

    What’s up to every one, for the reason that I am in fact keen of reading this website’s post to be updated regularly.
    It includes good information.

  • Colourhill

    August 28, 2020 - 10:17 pm

    I blog quite often and I really thank you for your content.
    This great article has truly peaked my interest. I
    will bookmark your blog and keep checking for new details about
    once a week. I opted in for your Feed too.

  • Leesa

    August 27, 2020 - 5:24 pm

    I’ve read some excellent stuff here. Certainly value bookmarking for revisiting.
    I wonder how a lot effort you set to make the sort of great informative web site.

  • Ned

    August 27, 2020 - 9:10 am

    Thank you for any other wonderful post. Where else could anyone get that kind
    of information in such an ideal approach of writing?
    I’ve a presentation subsequent week, and I’m on the look for such info.

  • Homes For Sale

    August 27, 2020 - 6:06 am

    It’s awesome for me to have a web page, which
    is helpful in favor of my experience. thanks admin

  • cristianphbsh.blogpostie.com

    August 27, 2020 - 2:09 am

    Way cool! Some extremely valid points! I appreciate you penning this post and the
    rest of the website is extremely good.

  • Karolyn

    August 26, 2020 - 8:14 pm

    My brother recommended I may like this website.

    He was totally right. This publish actually made my day.

    You cann’t believe just how so much time I had spent foor this info!

    Thanks!

  • antalya escort

    August 26, 2020 - 12:53 pm

    With havin so much content do you ever run into any problems of plagorism or copyright infringement?
    My site has a lot of exclusive content I’ve either written myself or outsourced but it
    looks like a lot of it is popping it up all over the
    internet without my authorization. Do you know any solutions to help stop
    content from being ripped off? I’d really appreciate
    it.

  • Cierra

    August 25, 2020 - 6:02 pm

    This site definitely has all of the information I wanted about this subject and
    didn’t know who to ask.

  • Slot

    August 24, 2020 - 8:06 pm

    These are really enormous ideas in concerning blogging. You have touched some pleasant factors here.
    Any way keep up wrinting.

  • pptx

    August 24, 2020 - 1:19 am

    Hello, I enjoy reading all of your article post.
    I like to write a little comment to support
    you.

  • mfm 7 robot

    August 23, 2020 - 2:58 pm

    I enjoy meeting utile information, this post has got me even more info!

  • gadai bpkb motor

    August 23, 2020 - 12:40 pm

    Terrific post but I was wanting to know if you could write a
    litte more on this topic? I’d be very grateful if you could elaborate a little
    bit further. Bless you!

  • Rahul Kumar

    August 23, 2020 - 2:31 am

    Hi,
    You can check the captcha plugin from here

  • Green Bay Seniors Home

    August 22, 2020 - 8:10 pm

    Hello everybody, here every one is sharing these kinds of know-how,
    therefore it’s pleasant to read this webpage, and I used to pay a quick
    visit this web site daily. https://npseniorliving.com/communities/howard/

  • официальный сайт 1xbet

    August 22, 2020 - 11:09 am

    continuously i used to read smaller articles or reviews
    that also clear their motive, and that is also happening with this article which I am
    reading at this time.

  • 대구출장안마

    August 21, 2020 - 6:08 pm

    Can you tell us more about this? I’d want to find out more details.

  • website

    August 21, 2020 - 2:23 am

    I’ve been browsing online more than 2 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. Personally, if all webmasters
    and bloggers made good content as you did, the web will
    be much more useful than ever before.

  • web page

    August 20, 2020 - 8:36 pm

    Hello There. I found your blog using msn. This is a really well written article.
    I’ll make sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will certainly return.

  • 유로247

    August 20, 2020 - 6:13 pm

    There is definately a lot to find out about this topic.

    I really like all the points you’ve made.

  • Google.site

    August 20, 2020 - 3:32 pm

    The other day, while I was at work, my cousin stole my iphone and tested to see if it
    can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad
    is now broken and she has 83 views. I know this is completely off topic but I had to share it with someone!

  • adult freindfinder

    August 20, 2020 - 10:16 am

    Good day! Do you use Twitter? I’d like to follow you
    if that would be okay. I’m undoubtedly enjoying your blog and
    look forward to new posts.

  • fb88

    August 20, 2020 - 7:15 am

    It’s very straightforward to find out any matter on net as compared to textbooks, as I found this
    paragraph at this website.

  • Canada immigration lawyers

    August 20, 2020 - 7:15 am

    Piece of writing writing is also a excitement, if you be acquainted with after that you can write if not it is difficult to write.

  • site

    August 20, 2020 - 2:33 am

    My partner and I stumbled over here different page and thought
    I might as well check things out. I like what I see so
    now i’m following you. Look forward to exploring your
    web page repeatedly.

  • poker uang asli

    August 19, 2020 - 9:36 pm

    Had no idea there was more money in the safe

  • back

    August 19, 2020 - 6:30 pm

    Thank you for sharing your thoughts. I really appreciate your efforts and I will be waiting for your next write ups thank you once again.

  • 광주출장마사지

    August 19, 2020 - 3:32 pm

    Massage therapy promotes tissue regeneration and is also a terrific way to
    cure acne, or even acne scars as well. When you visit a spa, the decisions about how you will spend your time
    and energy are often left for you but you could be wise to
    make a choice of these packages that incorporate numerous
    services from facials to mineral baths. Again, this process
    isn’t for anyone but should you prefer to wash your own personal sheets, this is the way
    you can keep them who is fit for a time.

  • web hosting quebec

    August 19, 2020 - 2:08 am

    Its like you learn my thoughts! You appear to grasp so
    much approximately this, such as you wrote the guide in it or something.
    I feel that you can do with a few p.c. to drive the message house a little
    bit, but other than that, this is great blog. A great read.
    I’ll definitely be back.

  • more information

    August 19, 2020 - 2:02 am

    I was recommended this website by means of my
    cousin. I am not positive whether this put up is written via him as
    no one else understand such designated approximately my
    difficulty. You are wonderful! Thanks!

  • đánh giá nhà cái

    August 19, 2020 - 1:17 am

    Everything is very open with a precise description of the challenges.
    It was really informative. Your website is very helpful.
    Thank you for sharing!

  • https://www.kp-lok.com/product/manifold-valve/

    August 18, 2020 - 11:41 pm

    Touche. Sound arguments. Keep up the good spirit.

  • 안전놀이터

    August 18, 2020 - 8:55 pm

    Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how can we communicate?

  • https://sites.google.com/view/freetiktokfollowers2020

    August 18, 2020 - 4:41 pm

    Undeniably believe that which you stated. Your favorite justification appeared to be on the internet the easiest thing to be aware of.
    I say to you, I certainly get irked while people think about worries that they just don’t know about.
    You managed to hit the nail upon the top and defined out
    the whole thing without having side-effects ,
    people can take a signal. Will probably be back to get more.
    Thanks

  • 赤身裸体

    August 18, 2020 - 6:31 am

    Yes! Finally someone writes about

  • Rahul Kumar

    August 18, 2020 - 1:05 am

    hu, You can use the Yoast SEO Plugin You can Download from here

  • 온라인슬롯머신사이트

    August 16, 2020 - 10:05 am

    I think this is one of the most significant info for me.
    And i’m glad reading your article. But wanna remark on some general things,
    The website style is ideal, the articles is really great : D.
    Good job, cheers

  • Judi Online

    August 16, 2020 - 1:19 am

    Nice post. I learn something totally new and challenging on websites I stumbleupon every day.
    It’s always helpful to read content from other authors and use something from other websites.

  • kant en klaar gordijnen

    August 15, 2020 - 11:44 pm

    I know this if off topic but I’m looking into starting my own weblog
    and was wondering what all is needed to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web savvy so I’m not 100% positive. Any
    recommendations or advice would be greatly
    appreciated. Many thanks

  • https://tuolumnecountyprofile.org

    August 15, 2020 - 11:12 am

    Hurrah! In the end I got a weblog from where I be able to genuinely obtain useful
    data regarding my study and knowledge.

    Here is my blog post: https://tuolumnecountyprofile.org

  • kant en klaar gordijnen

    August 15, 2020 - 10:44 am

    Hi there, everything is going well here and ofcourse every one is sharing information,
    that’s actually fine, keep up writing.

  • perfect beauty anti wrinkle eye cream

    August 15, 2020 - 6:52 am

    This is a topi which is coose to my heart… Take care! Exactly where are your
    contact details though?

    my site :: perfect beauty anti wrinkle eye cream

  • face mask with air valve

    August 15, 2020 - 6:06 am

    I always emailed this weblog post page to all my contacts, since if like to read it next my contacts will too.

  • website

    August 15, 2020 - 3:55 am

    Sweet blog! I found it while browsing on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

  • garden bridge sadiq khan

    August 14, 2020 - 9:30 pm

    Inspiring story there. What happened after? Good luck!

  • https://karmatantric.com/masseuse/ellie/

    August 14, 2020 - 8:11 pm

    Paragraph writing is also a excitement, if you be acquainted with after that you can write or else it is complex to write.

  • Lawanna

    August 14, 2020 - 3:41 am

    I am no longer certain where you’re getting your info, however good topic.
    I must spend a while finding out much more or working out more.

    Thanks for fantastic info I was looking for this info for my mission.

  • streaming

    August 14, 2020 - 12:01 am

    Hello there, just became aware of your blog through Google, and
    found that it’s truly informative. I’m going
    to watch out for brussels. I’ll be grateful if you continue this in future.
    Many people will be benefited from your writing.
    Cheers!

  • https://linktr.ee/agenpokervterpercaya

    August 13, 2020 - 8:32 am

    Howdy very nice blog!! Guy .. Excellent .. Wonderful .. I will bookmark your site
    and take the feeds additionally? I am satisfied to find numerous useful info right here in the put up, we’d like work out extra strategies on this regard, thank you
    for sharing. . . . . .

  • sbobet mobile

    August 12, 2020 - 9:15 pm

    Itu main di slot mana zeus?? Spad apa pragmtic

  • printing

    August 12, 2020 - 2:55 pm

    It’s perfect time to make some plans for the
    future and it is time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or advice.
    Perhaps you can write next articles referring to this
    article. I desire to read even more things about it!

  • little oral andie

    August 11, 2020 - 2:45 am

    I am sure this article has touched all the internet people,
    its really really nice post on building up new weblog.

  • 슬롯사이트

    August 10, 2020 - 11:19 pm

    Howdy just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Safari.
    I’m not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I’d post
    to let you know. The layout look great though! Hope you get the
    problem fixed soon. Cheers

  • bokep janda muda

    August 10, 2020 - 7:17 pm

    It’s a shame you don’t have a donate button! I’d definitely
    donate to this brilliant blog! I guess for
    now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to new updates and will share this site with my Facebook group.
    Chat soon!

  • royalrahul007

    August 10, 2020 - 1:16 am

    Hi Agen, you can use WordPress. Its good CMS for Blogging.

  • Judi Online Terpercaya

    August 9, 2020 - 3:53 pm

    Attractive element of content. I just stumbled upon your blog and in accession capital to claim that I get actually loved account your weblog posts.

    Any way I’ll be subscribing for your feeds or even I fulfillment you get right of entry to constantly quickly.

  • sjbphoto

    August 8, 2020 - 3:20 am

    I read this piece of writing fully regarding the resemblance of most
    up-to-date and previous technologies, it’s remarkable article.

Leave A Reply