PHP 5.3注意:HTML Select中的未定义索引

时间:2013-06-19 08:55:10

标签: php html select post

以下代码在php 5.3下生成注意:未定义索引:waehrungbep 。 使用error_reporting(^^E_NOTICE)我抑制了消息。 但是我如何修复代码而不是取消通知?

<label for="WährungBEP" class="label"></label>
<select name="waehrungbep" id="waehrungbep" title="Waehrung w&auml;hlen." onChange="return recalc()">
 <option value="1.22" <?php if($_POST['waehrungbep'] == "1.22") { echo "selected=\"selected\""; }; ?>selected >EUR</option>
 <option value="1" <?php if($_POST['waehrungbep'] == "1") { echo "selected=\"selected\""; }; ?>>CHF</option>
 <option value="0.95" <?php if($_POST['waehrungbep'] == "0.95") { echo "selected=\"selected\""; }; ?>>$</option>
</select>

2 个答案:

答案 0 :(得分:3)

更改

if($_POST['waehrungbep'] == "1.22")

if(isset($_POST['waehrungbep']) && $_POST['waehrungbep'] == "1.22")

答案 1 :(得分:1)

你可以试试这个:

<?php
    $waehrungbep = ( isset($_POST['waehrungbep']) ) ? $_POST['waehrungbep'] : false;
    function isSelected($var, $val, $default=false){
        $select = ($var == $val || ($default && $var === false) )  ? "selected=\"selected\"" : '';
        echo $select;
    }
?>
<label for="WährungBEP" class="label"></label>
<select name="waehrungbep" id="waehrungbep" title="Waehrung w&auml;hlen." onChange="return recalc()">
    <option value="1.22" <?php isSelected($waehrungbep, 1.22, true); ?>>EUR
    </option>
    <option value="1" <?php isSelected($waehrungbep, 1); ?>>CHF
    </option>
    <option value="0.95" <?php isSelected($waehrungbep, 0.95); ?>>$
    </option>
</select>

http://phpfiddle.org/main/code/esk-fz7

请记住,ternarys比if语句更快。 您还可以使用selected属性和selected =“selected”属性,这意味着您混合严格而非严格的html。