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条)

报告相同问题?