doudu5498 2014-06-22 19:01
浏览 30
已采纳

如果要处理实际的“<option>”而不是“value”属性,如何在PHP中处理选择框?

<span> Kamertype :</span>
        <select class="autowidthselect" required area-required="true" name="prijskamertype" onChange="calculatePrice()" id="prijskamertype">
            <option selected="selected" value="">Selecteer kamertype</option>
            <option value="295">Kraaiennest</option>
            <option value="395">Kajuit</option>
            <option value="495">Kapiteinshut</option>
        </select>

When I process the above with the PHP code below, it shows me the content defined in the 'value' attribute, which in this case are the prices. I actually want to show the content defined in the <option> tag (Kraaiennest, Kajuit, Kapiteinshut). How do I achieve that?

(I can not change the values, because of the calculation of the total price at the end of the form)

<?php
$prijskamertype = $_POST ['prijskamertype'];
echo $prijskamertype.' type';
?>

EDIT:

I have created an array and a drop-down list with it:

<?php
$optionskamertype = array();
$optionskamertype[""] = "Selecteer kamertype";
$optionskamertype["295"] = "Kraaiennest";
$optionskamertype["395"] = "Kajuit";
$optionskamertype["495"] = "Kapiteinshut";
?>

HTML-code:

        <span> Kamertype :</span>
        <select class="autowidthselect" required area-required="true" name="prijskamertype" onChange="calculatePrice()" id="prijskamertype">

             <?php
                foreach($optionskamertype as $key => $value)
                {
                    echo '<option value="'. $key .'" label="'. $value .'">'.$value.'</option>';
                }
             ?>

        </select>

What do I have to do now?

  • 写回答

2条回答 默认 最新

  • dpsyssiv90846 2014-06-22 19:19
    关注

    This isn't really to do with PHP, it's how HTML forms work: the only thing sent to the server by the browser is the content of the value attribute. (Or, if there isn't one, the content of the option tag, but that doesn't really help here because you still only get one value.)

    What you can do, though, is have an array in your PHP code that has the values and labels of all the options. You can use this to create the drop-down in the first place, and then when the form is submitted do something like this:

    $label = $option_list[ $_POST['whatever'] ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集