douluan5444 2011-06-19 12:38
浏览 69

PHP中的多重选择 - 如何编辑数据?

it is easy to create a form that will allow a user to select multiple values and then get the data that was entered. What I want to know is how to recreate that form based on the information in the database? ie Edit the data.

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
    <option value="four">four</option>
    <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

<?php
    $test=$_POST['test'];
    if ($test){
     foreach ($test as $t){echo 'You selected ',$t,'<br />';}
    }
?>

Say values two and foru were seleted in the original data. How would I recreate the form to edit the data like the code below:

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
    <option value="one">one</option>
    <option selected value="two">two</option>
    <option value="three">three</option>
    <option selected value="four">four</option>
    <option value="five">five</option>
</select>
<input type="submit" value="Send" />
</form>

Initially I want the code for add and edit to work. Eventually I want to be able to enter various multiple select options in a form and to edit them.

enter image description here

  • 写回答

1条回答 默认 最新

  • doushi1996 2011-06-19 13:15
    关注

    The easiest way to handle that is to store valid form values in an array.

    $hourList = range(1, 24);
    

    Then iterate over the array when printing

    foreach($hourList as $hour)
    {
      if ($hour == $enteredValue)
      {
        echo "<option selected value=\"{$hour}\">{$hour}</option>";
      }else
      {
        echo "<option value=\"{$hour}\">{$hour}</option>";
      }
    }
    

    If you're accepting multiple selections you may want to look into in_array() rather than direct comparison. For situations where you're offering a 'Every Month/day/year' option, simply prepend that to the array after using range.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)