Saturday, 18 June 2016

How to populate state dropdown based on option selected in country dropdown using jQuery



HTML



<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Populate City Dropdown Using jQuery Ajax</title>

<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>

<script type="text/javascript">

$(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>

PHP CODE

<?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>";

}

}

?>

Online Jquery Link

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

How to Select / Deselect All Checkboxes using jQuery

if you are newbie developer and looking for a quick jQuery snippet that selects and deselects multiple checkboxes by clicking “Select All” checkbox (exactly like in Gmail), here’s one that might be useful for your HTML form. I’ve used this one in various occasions and it has proved to be very consistent every time.

As you can see in the picture below, we have multiple checkboxes, and we want toggle the select state of checkboxes when we click “Select All” box.




JS


$(document).ready(function() {
$('#selecctall').click(function(event) { //on click
if(this.checked) { // check select status
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = true; //select all checkboxes with class "checkbox1"
});
}else{
$('.checkbox1').each(function() { //loop through each checkbox
this.checked = false; //deselect all checkboxes with class "checkbox1"
});
}
});

});


$(document).ready(function(){
$("#selecctall").change(function(){
$(".checkbox1").prop('checked', $(this).prop("checked"));
});
});

HTML


<ul class="chk-container">
<li><input type="checkbox" id="selecctall"/> Selecct All</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item1"> This is Item 1</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item2"> This is Item 2</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item3"> This is Item 3</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item4"> This is Item 4</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item5"> This is Item 5</li>
<li><input class="checkbox1" type="checkbox" name="check[]" value="item6"> This is Item 6</li>
<li><input class="checkbox2" type="checkbox" name="check[]" value="item6"> Do not select this</li>
</ul>