简体   繁体   中英

Echo selected in drop down menu

I am getting confused now. Why am I not able to echo an selected item?

                  echo'
                  <form class="form-horizontal form-calculator" id="interval" action="includes/toolbox_interval.php" method="post">
                  <div class="modal-body">
                      <div class="form-group">
                          <label for="interval">Kies interval</label>
                          <select class="form-control" name="interval" id="interval">
                            <option></option>
                            <option'; if($row['days'] == 7){ echo 'selected="selected"'; } echo 'value="7">Wekelijks</option>
                            <option value="14">2 wekelijks</option>
                            <option value="30">Maandelijks</option>
                            <option value="91">Elk kwartaal</option>
                            <option value="365">Jaarlijks</option>
                          </select>
                        </div>
                  </div>
                  </form>';

Muiter,

For one, you need spaces, try the following--

<option '; if($row['days'] == 7){ echo 'selected="selected"'; } echo ' value="7">Wekelijks</option>

Notice the space after option tag and before the value attribute

If you do not have the spaces, here are the scenarios you are facing...

  1. The if condition has met. As a result, your resulting option tag will be:

     <optionselected="selected"value="7">Wekelijks</option> 
  2. The if condition has not met. In which case, your resulting option tag will be:

     <optionvalue="7">Wekelijks</option> 

Hope this helps!

-Rush

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