-1
$sel="";
$suc="";
$gend="";
$male="";
$female="";
$selected_radio="";
if(isset($_POST['save']))
{
    $e=0;
    $selected_radio = $_POST['gender'];
    if($selected_radio!="")
    {
      if ($selected_radio == 'male')
      {
        $male = 'checked';
      }
      else if ($selected_radio == 'female')
      {
        $female = 'checked';
      }
    }
    else
    {
      $sel="Select Your Gender";
      $e=1;
    }
    if($e==0)
    {
      $suc="Success";
      $male ="";
      $female ="";
    }
}

在不选择选项/单选按钮的情况下,当我单击“保存”(提交按钮)时,它显示“未定义索引:性别”。但是错误消息也出现了"Select Your Gender"。当我选择任何一个选项(男/女)时,它会成功出现消息。(“成功”)。

所以请帮我找到解决方案

4

3 回答 3

1
    $sel="";
    $suc="";
    $gend="";
    $male="";
    $female="";
    $selected_radio="";
    if(isset($_POST['save']) && isset($_POST['gender']))
    {
     $e=0;
     $selected_radio = $_POST['gender'];

      if ($selected_radio == 'male')
      {
       $male = 'checked';
      }
      else if ($selected_radio == 'female')
      {
       $female = 'checked';
      }

   }
    else
    {
    $sel="Select Your Gender";
    $e=1;
    }
    if($e==0)
    {
    $suc="Success";
    $male ="";
    $female ="";
    }
于 2013-04-09T06:51:05.457 回答
1

如果您未选择/选中单选按钮/复选框,则特定值不会在$_POST全局数组中传递。

您可以使用isset检查是否设置了特定值。isset($_POST['gender'])

于 2013-04-09T06:52:29.090 回答
1

更改if(isset($_POST['save']))

if(isset($_POST['save']) && isset($_POST['gender']))

于 2013-04-09T06:52:52.730 回答