1

下面的代码在 php 5.3 下生成一个Notice: Undefined index: 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>
4

2 回答 2

3

改变

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

if(isset($_POST['waehrungbep']) && $_POST['waehrungbep'] == "1.22")
于 2013-06-19T08:55:55.443 回答
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

请记住,三元比 if 语句更快。您还使用 selected 属性和 selected="selected" 属性,这意味着您混合了严格和不严格的 html。

于 2013-06-19T09:47:17.240 回答