

How to populate state dropdown based on option selected in country dropdown usin...
source link: https://www.laravelcode.com/post/how-to-populate-state-dropdown-based-on-option-selected-in-country-dropdown-using-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to populate state dropdown based on option selected in country dropdown using jQuery
Use the jQuery ajax()
method
Populating the state or city dropdown based on the value of option selected by the user in the country dropdown is a very common implementation of Ajax feature that you have seen on many websites while filling the registration form. You can do this easily through the jQuery ajax()
method along with the little help of any server side scripting language like PHP.
Let's take a look at the following example to understand how it basically works:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Populate City Dropdown Using jQuery Ajax</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("select.country").change(function(){
var selectedCountry = $(".country option:selected").val();
$.ajax({
type: "POST",
url: "process-request.php",
data: { country : selectedCountry }
}).done(function(data){
$("#response").html(data);
});
});
});
</script>
</head>
<body>
<form>
<table>
<tr>
<td>
<label>Country:</label>
<select class="country">
<option>Select</option>
<option value="usa">United States</option>
<option value="india">India</option>
<option value="uk">United Kingdom</option>
</select>
</td>
<td id="response">
<!--Response will be inserted here-->
</td>
</tr>
</table>
</form>
</body>
</html>
The jQuery script above send the value of option selected in the country dropdown to the server. The "process-request.php" file on the server then process the request, if the request succeeds the jQuery script receives the returned data and creates the city dropdown list.
The PHP code inside the "process-request.php" file will look something like this:
<?php
if(isset($_POST["country"])){
// Capture selected country
$country = $_POST["country"];
// Define country and city array
$countryArr = array(
"usa" => array("New Yourk", "Los Angeles", "California"),
"india" => array("Mumbai", "New Delhi", "Bangalore"),
"uk" => array("London", "Manchester", "Liverpool")
);
// Display city dropdown based on country name
if($country !== 'Select'){
echo "<label>City:</label>";
echo "<select>";
foreach($countryArr[$country] as $value){
echo "<option>". $value . "</option>";
}
echo "</select>";
}
}
Author : Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
-
38
Country A searchable dropdown component to select a country code with react native ...
-
7
The default combo box has selected the last option advertisements I want a combobox by default selected the last option (using jquery):
-
4
Closed Bug 1722449 Opened 2 months ago Closed 1 month ago...
-
7
TikTok Tests New Option to Pin Selected Video Clips to the Top of Your Profile Published Sept. 20, 2021 By
-
8
TikTok's Testing a New 'Tips' Option for Selected Creators Published Oct. 27, 2021 By
-
8
native-base-select Select creates a dropdown list of items with the selected item in closed view. Native Base Select Dependencies ...
-
7
Details (Keywords: good-first-bug) User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100...
-
5
Angular Basics: How To Get the Value of a Selected Dropdown Menu Item ...
-
16
Narendra Singh Payak Nov 14, 2022 at 08:18 AM Unable to hide field "Rule Category" for selected Rule type (dropdown) as "QUID...
-
3
Select a Dropdown Option Using Selenium WebDriver in C#
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK