Populating dropdown - PHP Ajax MySQL

The  output



Step 1  Make City  table.


CREATE TABLE `City` (
  `ID` int(11) NOT NULL auto_increment,
  `Name` char(35) NOT NULL default '',
  `CountryCode` char(3) NOT NULL default '',
  `District` char(20) NOT NULL default '',
  `Population` int(11) NOT NULL default '0',
  PRIMARY KEY  (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



Step 2 : Write HTML Code

                          
     <select id="city" name="city" placeholder="Select City" class="form-control" >
</select>
                            


Step 3: Write php Code save as cityapi.php


<?php
$link = mysql_connect("localhost", "root", "");

   if (!$link)
   {
      die('Could not connect: ' . mysql_error());
   }
   if (mysql_select_db("myDatabase", $link))
   {
$q3=mysqli_query($this->con,"SELECT Name FROM City where countrycode =  'ind' order by name" )or die('Error157');
while($row1=mysqli_fetch_array($q3))
{
$city=$row1['Name'] ;
echo '<option>'.$city.'</option>';
}
}
?>

Step 3: Write ajax  Code


$(document).ready( function () {
  
  $.ajax({
type: "POST",
url: "cityapi.php",
 cache: false,
success: function(result){     
 $("#city").append(result);
              
                                                                }
 });
});
</script>


Note: CSS is missing Only the functionality is written.  



Comments

Popular posts from this blog

5 Mini HTML Projects for Beginners

What is HTML? Complete Beginner’s Guide (with Examples)

HTML Forms — Input, Label, Button & Validation (Explained Like a Teacher With Examples)