简体   繁体   中英

populate 2nd dropdown by select first dropdown value in php

I am declared two select. once i select value in first select then i will fetch value in second select.

<?php
$result1 = mysql_query("SELECT row1 FROM information");?>
<select id='select1'>
<?php
while($row=mysql_fetch_array($result1))
{
if($row['row1']!=NULL)
{
echo "<option value='$row[row1]'>$row[row1]</option>";
}
}?>
</select><br/><br/>
<?php
//*******************************************  
$result1 = mysql_query("SELECT column1 FROM information");
echo "<select id='select2'>";
while($row=mysql_fetch_array($result1))
{
if($row['column1']!=NULL)
{
echo "<option value='$row[column1]'>$row[column1]</option>";
}
}?>
</select>

You have two possibilities:

  1. Use jQuery + ajax to build dynamically second select after first (onselect event)
  2. Submit form or redirect user to same page with selected value from first select and if it is given (check in PHP from $_GET or $_POST) and if it is - generate second select with this info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM