Follow my blog with Bloglovin
Two Drop Down with PHP Mysql AJax Code,Need to some basic syntax of ajax for call to ajax and need some basic jquery
1 – config.php
for connection to mysql
<?php $connect = mysql_connect('localhost','root',''); if (!$connect) { die('Could not connect to MySQL: ' . mysql_error()); } mysql_select_db('test_database',$connect); ?>
2 – test.php
<?php include('config.php'); ?> <html> <head> <script src="jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $("#country").change(function(){ var country = $("#country").val(); if(country!='') { $.ajax({ type:"post", url:"state.php", data:{country_code:country}, cache: false, dataType:'html', success:function(data){ if(data){ $("#state").hide(); $("#state_res").html(data); } } }); } }); }); </script> </head> <body> <table cellpadding="10" cellspacing="10" width="50%"> <tr> <td>Select Country</td> <td> <td> <select name="country" id="country" ><option>Select Country</option> <?php $sql=mysql_query("Select * from country"); while($row=mysql_fetch_array($sql)) { ?> <option value="<?php echo $row['id']?>"><?php echo $row['country_name']?></option> <?php }?> </select> </td> <td>State</td> <td> <span id="state_res"></span> <span id="state"> <select> <option value="">Select State1</option> </select> </span> </td> </tr> </table> </body> </html>
3 – state.php
<?php include('config.php'); $id = $_REQUEST['country_code'];?> <select name="state" id="state"> <option value="">Select State</option> <?php $res = mysql_query("SELECT * from state where country_id=$id"); while($row = mysql_fetch_assoc($res)){?> <option value="<?php echo $row['id'];?>"><?php echo $row['state_name'];?></option> <?php }?> </select>