On this tutorial, we talk about learn how to change the nation state metropolis dropdown checklist in PHP utilizing jQuery ajax from the MySQL database.
On this instance, we’ll information you step-by-step on to alter nation state metropolis within the dropdown checklist on change in PHP from MySQL utilizing jQuery.
Drop down checklist utilizing PHP and MySQL by JQuery will look Like as observe.
First, create HTML Kind with three choose fields.
</desk>
</type>
</div>
</div>
<hr>
</div><!–/center–>
<!–/right–>
<hr>
</div>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
div class=“container-fluid”> <!—middle—> <div class=“col-sm-8”> <div class=“row”> <div class=“col-xs-12”> <h3>jQuery Dependent DropDown Listing – Nation, States and Metropolis </h3> <hr > <type identify=“insert” motion=“” technique=“put up”> <desk width=“100%” top=“117” border=“0”> <tr> <th width=“27%” top=“63” scope=“row”>Nation :</th> <td width=“73%”><choose onChange=“getstate(this.worth);” identify=“nation” id=“nation” class=“form-control” > <choice worth=“”>Choose</choice> <?php $question =mysqli_query($con,“SELECT * FROM nation”); whereas($row=mysqli_fetch_array($question)) { ?> <choice worth=“<?php echo $row[‘id’];?>“><?php echo $row[‘countryname’];?></choice> <?php } ?> </choose> </td> </tr> <tr> <th width=“27%” top=“63” scope=“row”>State :</th> <td width=“73%”> <choose identify=“statelist” id=“statelist” onChange=“getcity(this.worth);” class=“form-control” > <choice worth=“”>Choose State</choice> </choose></td> </tr> <tr> <th scope=“row”>Metropolis :</th> <td><choose identify=“metropolis” id=“metropolis” class=“form-control”> <choice worth=“”>Choose Metropolis</choice> </choose></td> </tr>
</desk> </type> </div> </div> <hr>
</div><!—/middle—> <!—/proper—> <hr> </div> |
Getting States utilizing jQuery AJAX
This script incorporates a operate that might be referred to as on altering nation dropdown values. It would ship AJAX request to a PHP web page to get corresponding State dropdown choices.
<script> operate getstate(val) { //alert(val); $.ajax({ kind: “POST”, url: “get_state.php”, information:“coutrycode=”+val, success: operate(information){ $(“#statelist”).html(information); } }); } </script> |
Learn State Database utilizing PHP
This PHP code connects the database to retrieve state desk values primarily based on the nation id handed by jQuery AJAX name.
?>
<?php require_once(“config.php”); if(!empty($_POST[“coutrycode”])) { $question =mysqli_query($con,“SELECT * FROM state WHERE countryid = ‘” . $_POST[“coutrycode”] . “‘”); ?> <choice worth=“”>Choose State</choice> <?php whereas($row=mysqli_fetch_array($question)) { ?> <choice worth=“<?php echo $row[“StCode”]; ?>“><?php echo $row[“StateName”]; ?></choice> <?php } }
?> |
Getting States utilizing jQuery AJAX
This script incorporates a operate that might be referred to as on altering states dropdown values. It would ship AJAX request to a PHP web page to get corresponding metropolis dropdown choices.
<script> operate getcity(val) { //alert(val); $.ajax({ kind: “POST”, url: “get_city.php”, information:“statecode=”+val, success: operate(information){ $(“#metropolis”).html(information); } }); } </script> |
Learn Metropolis Database utilizing PHP
This PHP code connects the database to retrieve metropolis desk values primarily based on the state id handed by the jQuery AJAX name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php require_once(“config.php”); if(!empty($_POST[“statecode”])) { $statecode=$_POST[“statecode”]; $query1 =mysqli_query($con,“SELECT metropolis.id as cityid,metropolis.cityname FROM metropolis be a part of state on state.StCode=metropolis.stateid be a part of nation on nation.id=metropolis.countryid WHERE metropolis.stateid = ‘$statecode'”); ?> <choice worth=“”>Choose Metropolis</choice> <?php whereas($row1=mysqli_fetch_array($query1)) { ?> <choice worth=“<?php echo $row1[“cityid”]; ?>“><?php echo $row1[“cityname”]; ?></choice> <?php } } ?>
|
How you can Run the Script
Obtain the script zip after that extract the zip file
Copy countrystatecitydropdown
folder and paste within the root listing
Now create the database with the identify demosdb
after that import the SQL file out there within the script folder
Now Run the script http://localhos/countrystatecitydropdown/