0

How to create a form with 2 drop down list box in PHP, which is the second "drop down list box" relies on the results of the first "drop down list box"? I apologize if this question has been asked, but I could not find a similar question. Thank you in advance.

Sory, i'm a newbie. Here is my Code:

<script>
function reloadPage()
  {
  location.reload();
  }
</script>
<form name="form1" action="updateitemstock.php" method="post">
 <table border="0" width="50%">
  <tr>
    <td width="18%"><b><font face="Verdana" size="2" color="#FFFFFF">Select 
Vendor</font></b></td>
    <td width="18%"><select size="1" id=name="dvendor">
<option selected>Choose Vendor Name</option>
    <?php
    while ($row = mysql_fetch_array($vendor_list)) { 
    ?>
 <option value="<?php echo $row[0]; ?>" onclick="reloadPage()"><?php echo $row[0]; ?></option>
<?php
    }
    ?>
    </select></td>
  </tr>
  <tr>
    <td width="18%"><b><font face="Verdana" size="2" color="#FFFFFF">Select 
Item</font></b></td>
    <td width="18%"><select size="1" name="ditem">
    <option selected>Choose Item Name</option>
    <?php
    if (isset($_POST['dvendor']) {
   $item_list = mysql_query("SELECT DISTINCT itemdesc FROM item WHERE dealer=$_POST['dvendor']");
  }
  while ($row = mysql_fetch_array($item_list)) { 
    ?>
          <option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
    <?php
  }
    ?>
    </select></td>
  </tr>
  <tr>
    <td width="18%" colspan="2">
    <div align="center">
  <table border="0">
   <tr>
    <td>
    <input type="submit" value="Save" name="bsave"></td>
     <td>
    <input type="submit" value="Upload" name="bupload"></td>
   </tr>
  </table>
    </div>
    </td>
  </tr>
 </table>
 </form>
Baskoro
  • 29
  • 4

3 Answers3

0

The two lists can be, besides, created in PHP, yet their data has to rely on a client side language, eg. JS or jQuery. For example, you want to create two lists, say, one for list of states and other for cities in the state chosen from the previous list. In that case, you will initially fill the list for states using PHP and in list for states, you will create a blank template (with no 'option' element in list). Now, at the user end, when a user selects a state, you can send an AJAX request to your server and get list of cities for that chosen state. When the request completes, you can populate the cities list with data returned by your server. Alternatively, you can create a single list for state and as many lists for cities as many are the states. Each of this list will contain cities for one state respectively. Now, at the client side, you can bind a 'change' event to states list where only a single cities list will be shown to user at a time as per state chosen by him. Besides this approach is not suggested, this approach depreciates the need of any AJAX requests, but then you have to create all the list for once at the backend. I hope that helps :)

Sayed
  • 601
  • 6
  • 21
0

Give this javascript Cascading Dropdown a go: http://jquery-plugins.net/jquery-cascading-dropdown-plugin

Stu1986C
  • 1,452
  • 1
  • 18
  • 34
0

There is a jQuery plugin which will be helpful. For the dynamic select, use the remote version.

Chained Selects Plugin for jQuery

Hope this helps :)

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54