public
Created

dynamic dropdown with php?

  • Download Gist
php_dynamic_dropdown.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 
<!-- this is first dropdown, second dropdown will be based on selection from this dropdown -->
<form method="post" action="">
<select name="first_dropdown">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
<input type="submit" />
</form>
<?php
if (isset($_POST['first_dropdown']) && !empty($_POST['first_dropdown']) ):
?>
 
 
<select name="second_dropdown">
 
<?php
//if user select a
if ($_POST['first_dropdown'] == "a"):
?>
<option value="a1">a1</option>
<option value="a2">a2</option>
<option value="a3">a3</option>
<?php endif; ?>
 
<?php
//if user select b
if ($_POST['first_dropdown'] == "b"):
?>
<option value="b1">b1</option>
<option value="b2">b2</option>
<option value="b3">b3</option>
<?php endif; ?>
 
<?php
//if user select c
if ($_POST['first_dropdown'] == "c"):
?>
<option value="c1">c1</option>
<option value="c2">c2</option>
<option value="c3">c3</option>
<?php endif; ?>
 
</select>
 
<?php endif; ?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.