Echo根据动态选择框中的$ _GET值选择

时间:2015-06-10 09:58:23

标签: php echo selected

我想将选择框作为GET变量中的选定值。 这是我的代码:

<select name="suraNo" id="suraNo" required="required">
  <option value="">-Select-</option>
    <?php
      do {  
        ?>
          <option value="<?php echo $row_SuraList['suraNo']?>">
            <?php echo $row_SuraList['suraNo']?>. <?php echo $row_SuraList['suraNameBN']?>
          </option>

          <?php
         } while ($row_SuraList = mysql_fetch_assoc($SuraList));

         $rows = mysql_num_rows($SuraList);
         if($rows > 0) {
           mysql_data_seek($SuraList, 0);
           $row_SuraList = mysql_fetch_assoc($SuraList);
         }
    ?>
</select> 

1 个答案:

答案 0 :(得分:0)

如果您想要的是从列表中选择与您的GET参数对应的值,只需使用:

<option <?php echo ($_GET['yourval'] == $row_SuraList['suraNo'] ? "selected" : null); ?> value="<?php echo $row_SuraList['suraNo']?>"><?php echo $row_SuraList['suraNo']?>. <?php echo $row_SuraList['suraNameBN']?></option>

另外,我建议你不要在你的...中关闭你的php应用程序,并写下你的选项&#34;那样:

echo "<option " . ($_GET['yourval'] == $row_SuraList['suraNo'] ? "selected" : null) . " value=\"" . $row_SuraList['suraNo'] . "\">" . $row_SuraList['suraNo'] . "." . $row_SuraList['suraNameBN'] ."</option>";