dongzhuo6137 2014-03-13 02:58
浏览 17

如何创建PHP下拉列表

I am trying to build a php contact form with a dropdown list of values, say "A", "B" and "C", that fits the code structure below.

As well, I would like to call this form from a link on another page, and have the value of the dropdown list automatically populated with the passed-in parameter. For example, if the link is "...?val=B", I'd like the dropdown list to automatically set to "B".

I have code below that works for an email field. What does the code look like for a dropdown list?

It's been a long while since I've coded PHP, so any help is appreciated.

<div class="input-field">
<div>
<?php _e('Email','circles'); echo ' <span>('; _e('required','circles'); echo ')</span>'; ?>
</div>
<div class='input-style dlight-grey sc-input'>
<input type="email" name="form_email" size="20" value="<?php echo $form_email; ?>" />
</div>
</div>
  • 写回答

1条回答 默认 最新

  • donglun4682 2014-03-13 03:53
    关注

    Suppose you store options to a html select element(dropdown list) in array $ops

    <form>
       <select name='foo'>
          <?php
          $val = $_GET['val'];
          while($option = next($ops)){
             if ($option == $val){
                echo "<option value=$option selected='selected'>$option</option";
             else
                echo "<option value=$option>$option</option>";
          }
          ?>
        </select>
    <form>
    

    Alternatively, you can use Javascript to add 'selected' attribute to the option equals to $_GET['val'], see .attr()

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮