注意select选项中的未定义索引

时间:2013-04-16 05:35:57

标签: php select indexing undefined option

<?php
$sel1="";
$sel2="";
$suc="";
$male="";
$female="";
$selected_radio="";
if(isset($_POST['save']))
{
    $e=0;
    if(isset($_POST['sport']))
    {
        $select_sport=$_POST['sport'];
        if($_POST['sport']=="")
        {
            $sel1="<--Select The Game-->";
            $e=1;
        }
    }
    if(isset($_POST['gender']))
    {
        $selected_radio = $_POST['gender'];
    }
    else
    {
        $sel2="<--Select Your Gender-->";
        $e=1;
    }
if($e==0)
{
$suc="Success";
$male ="";
$female ="";
}
}
?>
<body>
<select name="sport">
<option value="">
<option value="cri" <?php if($select_sport=='cri') { echo "selected"; } ?>> CRICKET </option>
<option value="foot" <?php if($select_sport=='foot') { echo "selected"; }?>> FOOTBALL </option>
</select>

当我在浏览器上打开此程序时,错误“未定义的索引”出现在SELECT选项(下拉框)中。

但是当我点击保存按钮时,错误消失了。当我没有选择任何运动时,它工作正常,它显示错误消息。


所以请帮我找到解决方案......

1 个答案:

答案 0 :(得分:-1)

如果未设置$select_sport,则未定义变量$_POST['sport']

要修复代码,请在开头添加以下内容:

$ select_sport ='';