By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

Two drop down boxes in php and mysql

100+
P: 250
In my application I have two drop down boxes. 2nd drop down box should filled according to the selected value on the 1st drop down box. But My 2nd drop down box not update.This is my code

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.     function showSelected(val){
  3.         document.getElementById('selectedResult').innerHTML = "The selected number is - " + val;
  4.     }
  5.     function show(val){
  6.         document.getElementById('model').innerHTML = "The selected number is - " + val;
  7.     }
  8. </script>
  9. <td>Make :</td> 
  10. <td>
  11.  
  12. <select name="make"  class="normalTxt" onChange='showSelected(this.value)' >
  13. <?php    $mysql_result1 = mysql_query("SELECT DISTINCT vehicleMake FROM  vehicles");
  14. while($row = mysql_fetch_array($mysql_result1)){
  15. foreach( $row AS $key => $val )
  16. $Make = stripslashes( $val );
  17.  
  18. ?>
  19. <option  value=<?php  echo  "'$Make'" ?> ><?php echo "$Make&nbsp;&nbsp;&nbsp;"?>
  20.  <?php 
  21.     }    
  22. ?>
  23.  </option>
  24. </select>
  25.  <?php
  26.  
  27. ?>
  28. </td>
  29. <div id='selectedResult'></div>
  30.  
  31.   <td>Model :</td> 
  32.     <td>
  33.  
  34.  
  35. <select name="vehicleModel" class="defaultBlackText" onChange='show(this.value)'>
  36. <option value="Select" selected="selected">Select</option>
  37. <?php
  38.  
  39. $qq = mysql_query("SELECT * FROM vehicles WHERE vehicleMake= '".$Make."' ");
  40. while($row = mysql_fetch_array($qq)){
  41. foreach( $row AS $key => $val ){
  42. $$key = stripslashes( $val );
  43. }?>
  44. <option> <?php 
  45. echo "$vehicleModel"; 
  46. ?> 
  47. </option>
  48. <?php }
  49. //}?>
  50.  
  51. </select>
  52. </td>
  53. <div id='model'></div>
  54.  
Please help me..
Jul 15 '11 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,365
PHP does not run on the client side. Either you have to submit the page to itself and pass the value that was selected or you send an HTTP request to a different PHP page that will return the values. The first option is easier but forces a refresh of the entire page every time the user selects a make. The second option requires more javascript and an additional PHP page but is more fluid.
Jul 15 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.