Adding select option dynemically in php with mysql

Just Check out my this post. Its all about dynamically generation of select option. when we select the particular country it should generate automatically the new select option of the states of the particular country.

Create first connectivity.php file. following is the coding for connectivity.

<?php
$con=mysql_connect("Localhost","root");
mysql_select_db("jobsindia",$con);
?>

Now goto localhost/phpmyadmin.

Create database named jobsindia.

Create tables named location_country, location_state

mysql_table

mysql_2

<?php
include 'connectivity.php';
$location=$_GET['loc'];
$email=$_GET['email'];
$paasword=$_GET['pwd'];
?><head>
<script type="text/javascript">
function reload(form)
{
var val=form.loc.options[form.loc.options.selectedIndex].value;
var pwd=document.form.pwd.value;
var email=document.form.email.value;
self.location='registration.php?loc=' + val+"&email="+ email+"&pwd="+pwd;

}
</script>

</head>
<body class="body1">
<form name="form" method="post">
 <td>Where are you currently Located:*</td>
 <td><select name="loc" id="location" onChange="reload(this.form)">
<?php


 $query="select name,id from location_country";
 $result=mysql_query($query);
 if($result)
 {
 ?>
 <option value="">SELECT</option>
 <?php
 while($row=mysql_fetch_array($result))
 {
 if($row[0]==$location)
 {
 
 ?>
 <option value="<?php echo $row[0]; ?>" selected><?php echo $row[0]; ?></option>
 <?php
 }
 else
 {
 ?>
 <option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
 <?php
 }
 }
 
 }
 

 ?>
 </select>
<?php 
 $query_country="select id from location_country where name='$location'";
 $result_country=mysql_query($query_country);
 if($result_country)
 {
 $id_country=mysql_fetch_array($result_country);

 }
 ?> 

 
 
 </td>
 <td>&nbsp;</td>
 </tr>
 <?php
 if(!$location=="")
 {
 if($location=="India")
 {
 ?>
 <tr>
 <td>Select Your State:*</td>
 <td><select name="state">
 <?php

 $state_query="select name_state from location_state where id=1";
 $result_state=mysql_query($state_query);
 if($result_state)
 {
 ?>
 <option value="">SELECT</option>
 <?php
 while($row_state=mysql_fetch_array($result_state))
 {
 ?>
 <option value ="<?php echo $row_state[0];?>"> <?php echo $row_state[0];?></option>
 <?php
 }
 
 }
 else
 {
 echo "ERROR";
 }
 ?>
 </select></td>
 <td>&nbsp;</td> 
 </tr>
 <?php
 }
 }
 ?>
<?php
 if(!$location=="")
 {
 if($location=="India")
 {
 ?>
 <tr>
 <td>Select Your State:*</td>
 <td><select name="state">
 <?php

 $state_query="select name_state from location_state where id=1";
 $result_state=mysql_query($state_query);
 if($result_state)
 {
 ?>
 <option value="">SELECT</option>
 <?php
 while($row_state=mysql_fetch_array($result_state))
 {
 ?>
 <option value ="<?php echo $row_state[0];?>"> <?php echo $row_state[0];?></option>
 <?php
 }
 
 }
 else
 {
 echo "ERROR";
 }
 ?>
 </select></td>
 <td>&nbsp;</td> 
 </tr>
 <?php
 }
 }
 ?>
</table>
</form>
</body>




 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s