Jump to content

Dynamic dependent Select boxes with PHP & MYSQL


palash4003

Recommended Posts

Dear Support,

 

Here is my code:

<?php 
    if ( strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {
        $name = isset($_POST['name]) ? htmlspecialchars($_POST['name']) : "";
	$area = isset($_POST['area']) ? htmlspecialchars($_POST['area']) : "";
                $choice = isset($_POST['choice ']) ? htmlspecialchars($_POST['choice ']) : "";

          //Do Others  
          }
?> 

<form class="customersForm" method="post">
        <div class="">
	  <p>Name<br />
	       <select style="width:120px;" name="name">
	          <?php
                               $sql = "select * from `name_info` order by `name`";
                               $query = mysql_query($sql);
                       $dd = '<option>Name</option>';				 
                               while($row = mysql_fetch_object($query)) {
                                    $dd .= '<option>'.$row->name.'</option>';
                               }
	               echo $dd;              
	          ?>
	      </select>
	     </p>
	  </div>

             <div class="">
	  <p>Area<br />
	       <select style="width:120px;" area="area">
	          <?php
                               $sql = "select * from `area_info` order by `area`";
                               $query = mysql_query($sql);
                       $dd = '<option>Area</option>';				 
                               while($row = mysql_fetch_object($query)) {
                                    $dd .= '<option>'.$row->area.'</option>';
                               }
	               echo $dd;              
	          ?>
	      </select>
	     </p>
	  </div>


            <div class="">
	  <p>Choice<br />
	       <select style="width:120px;" choice="choice">
	          <?php
                               $sql = "select * from `choice_info` where `name`='".$name."' and `area`='".$area."' order by `choice`";
                               $query = mysql_query($sql);
                       $dd = '<option>Choice</option>';				 
                               while($row = mysql_fetch_object($query)) {
                                    $dd .= '<option>'.$row->choice.'</option>';
                               }
	               echo $dd;              
	          ?>
	      </select>
	     </p>
	  </div>

......//Others input fields ...some text fields ...

</ form>

 

I have the above code, where I would like to show the choice Drop-down box, depends on $name and $area match of the choice_info table. $name field and $area field needs to have any dependency and straight from the MySQL table. But, choice needs to depend on $name & $area. How can I do it without much using Javacript? I do not mind to use JavaScript, but prefer to minimal.

 

By the way, I will have some other input fields too. It's an input form but, one drop-down box will be depends on two others choice.

 

Thanks for your help.

 

 

 

 

  • 9 months later...

Dynamic Dependent Select Boxes

 

How to create dependent drop down list in php?

I knew it was possible to create a dynamic dependent drop down form using PHP, MySQL, and Javascript,

but I had never had an excuse to use one before I need to teach to my students.

<?php

$region = $country = $state = $city = null; //declare vars

$conn = mysql_connect('localhost', 'username', 'password');
$db = mysql_select_db('selects',$conn);

if(isset($_GET["region"]) && is_numeric($_GET["region"]))
{
   $region = $_GET["region"];
}

if(isset($_GET["country"]) && is_numeric($_GET["country"]))
{
   $country = $_GET["country"];
}

if(isset($_GET["state"]) && is_numeric($_GET["state"]))
{
   $state = $_GET["state"];
}

if(isset($_GET["city"]) && is_numeric($_GET["city"]))
{
   $city = $_GET["city"];
}

?>

<script language="Javascript">

function autoSubmit()
{
   var formObject = document.forms['theForm'];
   formObject.submit();
}

</script>

<form name="theForm" method="get">

   <!-- REGION SELECTION -->

   <select name="region" onchange="autoSubmit();">
       <option [url=""]value[/url]="null"></option>
       <option VALUE="1" <?php if($region == 1) echo " selected"; ?>>East</option>
       <option VALUE="2" <?php if($region == 2) echo " selected"; ?>>West</option>
   </select>

   <br><br>

   <!-- COUNTRY SELECTION BASED ON REGION VALUE -->

   <?php

   if($region != null && is_numeric($region))
   {

   ?>

   <select name="country" onchange="autoSubmit();">
       <option VALUE="null"></option>

       <?php

       //POPULATE DROP DOWN MENU WITH COUNTRIES FROM A GIVEN REGION

       $sql = "SELECT COUN_ID, COUN_NAME FROM COUNTRY WHERE RE_ID = $region";
       $countries = mysql_query($sql,$conn);

       while($row = mysql_fetch_array($countries))
       {
           echo ("<option VALUE=\"$row[COUN_ID]\" " . ($country == $row["COUN_ID"] ? " selected" : "") . ">$row[COUN_NAME]</option>");
       }

       ?>

   </select>

   <?php

   }

   ?>

   <br><br>

   <?php

   if($country != null && is_numeric($country) && $region != null)
   {

   ?>

   <select name="state" onchange="autoSubmit();">
       <option VALUE="null"></option>

       <?php

       //POPULATE DROP DOWN MENU WITH STATES FROM A GIVEN REGION, COUNTRY

       $sql = "SELECT STAT_ID, STAT_NAME FROM states WHERE COUN_ID = $country ";
       $states = mysql_query($sql,$conn);

       while($row = mysql_fetch_array($states))
       {
           echo ("<option VALUE=\"$row[sTAT_ID]\" " . ($state == $row["STAT_ID"] ? " selected" : "") . ">$row[sTAT_NAME]</option>");
       }

       ?>    

   </select>

   <?php

   }

   ?>

   <br><br>

   <?php

   if($state != null && is_numeric($state) && $region != null && $country != null)
   {

   ?>

   <select name="city" onchange="autoSubmit();">
       <option VALUE="null"></option>

       <?php

       //POPULATE DROP DOWN MENU WITH CITIES FROM A GIVEN REGION, COUNTRY, STATE

       $sql = "SELECT CIT_ID, CITY_NAME FROM CITY WHERE STAT_ID = $state ";
       $cities = mysql_query($sql,$conn);

       while($row = mysql_fetch_array($cities))
       {
           echo ("<option VALUE=\"$row[CIT_ID]\" " . ($city == $row["CIT_ID"] ? " selected" : "") . ">$row[CITY_NAME]</option>");
       }

       ?>    

   </select>

   <?php

   }

   ?>

</form>

 

For complete code visit: http://ashishrevar.com/2012/01/dynamic-dependent-select-boxes/

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.