简体   繁体   中英

dropdown menu doesn't function

my problem is when i select the monthly part, the next drop down menu doesn't come out. the first drop down is work well but when i select the first selection it does not display anything. this is my code

  <td><h4 class="text-login">By Category &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h4></td> <td> <select name="selection"> <option value="">--Select--</option> <option value="1" id="month" type="select" >Monthly</option> <option value="2" id="year" type="select" >Yearly</option> </select> </td> <?php if(isset($_POST['Submit'])) { if(isset($_POST['selection']) && $_POST['selection'] == '1') { echo '<td><h4 class="text-login">Please select month &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h4></td>'; echo '<td>'; echo '<select name="month">'; echo '<option value="">--Select--</option>'; echo '<option value= "January" name = "first" id="month1" type="select" >January</option>'; echo '<option value="b2" id="month2" type="select" >February</option>'; echo '<option value="b3" id="month3" type="select" >March</option>'; echo '<option value="b4" id="month4" type="select" >April</option>'; echo '<option value="b5" id="month5" type="select" >May</option>'; echo '<option value="b6" id="month6" type="select" >June</option>'; echo '<option value="b7" id="month7" type="select" >July</option>'; echo '<option value="b8" id="month8" type="select" >August</option>'; echo '<option value="b9" id="month9" type="select" >September</option>'; echo '<option value="b10" id="month10" type="select" >October</option>'; echo '<option value="b11" id="month11" type="select" >November</option>'; echo '<option value="b12" id="month12" type="select" >December</option>'; echo '</select>'; echo '<tr>'; echo '<td><h4 class="text-login">Lab &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h4></td>'; echo '<td>'; $sql = "SELECT location FROM item "; $result = mysqli_query($conn,$sql); $rownum = mysqli_num_rows($result); echo '<select name="lab" >'; echo '<option value="">--Select--</option>'; $i=0; while ($row = mysqli_fetch_assoc($result)) { echo '<option value="" id="lab1" type="select"><?php echo $row["location"];?></option>'; $i++; } echo '</select>'; echo '</td><tr><td align="right">'; echo '</table></center>'; } else { echo '<td><h4 class="text-login">Please select month/year &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</h4></td>'; echo '<td>'; echo '<select name="" onchange = "">'; echo '<option value="">--Select--</option>'; echo '<option value="t8" id="year8" type="select" >2000</option>'; echo '<option value="t9" id="year9" type="select" >2001</option>'; echo '<option value="t10" id="year10" type="select" >2002</option>'; echo '<option value="t11" id="year11" type="select" >2003</option>'; echo '<option value="t12" id="year12" type="select" >2004</option>'; echo '<option value="t12" id="year12" type="select" >2005</option>'; echo '<option value="t12" id="year12" type="select" >2006</option>'; echo '<option value="t12" id="year12" type="select" >2007</option>'; echo '<option value="t12" id="year12" type="select" >2008</option>'; echo '<option value="t12" id="year12" type="select" >2009</option>'; echo '<option value="t12" id="year12" type="select" >2010</option>'; echo '<option value="t12" id="year12" type="select" >2011</option>'; echo '<option value="t12" id="year12" type="select" >2012</option>'; echo '<option value="t12" id="year12" type="select" >2013</option>'; echo '<option value="t12" id="year12" type="select" >2014</option>'; echo '<option value="t12" id="year12" type="select" >2015</option>'; echo '</select>'; echo '</td><tr><td align="right">'; } } ?> </tr> </table></center> 

Remove id or type

<select name="selection">
<option value="">--Select--</option>
<option value="1">Monthly</option>
<option value="2" >Yearly</option>  
</select>

It appears that you want the second dropdown once you have selected the first - to get it you must submit the page - the crudest way would be to put that onto the first dropdown's onChange event. Make sure you have a form to submit.

 <form name="form" id="form" action="your_sql_page.php" method="get">

        <select name="selection" onChange="submit();">
        <option value="">--Select--</option>
        <option value="1">Monthly</option>
        <option value="2">Yearly</option>  
        </select>
  </form>

How to submit form on change of dropdown list?

It would probably be best to have the second dropdown triggered by JavaScript - unless you are worried about not being dependent on JavaScript as per the comments on the reference page.

If you are using php it needs to be submitted to the server - this reference explains the process: Don't know how to solve (Between Php and Javascript)

The form's action could be to submit to itself - you will need to add onChange and submit() to the second dropdown

     echo '<select name="yearly" onchange = "submit()">';

and add code that will make the page respond differently, depending on what has been submitted - wrap it in an if(isset(...

action="<?php echo %_SERVER["PHP_SELF"]; ?>"

As per @Rahautos do remove id and type .

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