怎么做基于表单元素的if语句(这有点难以解释)

时间:2013-12-30 14:21:48

标签: php forms validation redirect

下面的代码说明了我的问题,我希望当性别设置为男性且选项设置为子时,将用户重定向到另一个页面。 谢谢

<?php
$gender = $age = "";
$age = $_POST['age'];

if($_SERVER["REQUEST_METHOD"]=="POST")
{
if (empty($_POST['gender']))
  {
  $gendererr = "Please fill out your gender...";
  }


if ($age == "SELECT")
  {
  $ageerr = "Please select if you are a child or an adult...";
  }

if ($age === $_POST['child'] && $gender === $_POST['male'])
  {
  header('Location: childfather.html');
  }
}
?>

这是HTML:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="forms-background">
<span class="error"><?php echo $ageerr;?></span>
<br>
<select name="age">
<option name="option">SELECT</option>
<option name="child">Child</option>
<option name="adult">Adult</option>
</select>
<br>
<span class="error"><?php echo $gendererr;?></span><br>
<label>Male</label>
<input type="radio" name="gender" id="male" value="male"><br>
<label>Female</label>
<input type="radio" name="gender" id="female" value="female"><br>
<input type="submit" value="submit" class="submit">
</div>
</form>

4 个答案:

答案 0 :(得分:0)

尝试

if ($_POST['age']=='child' && $_POST['gender']=='male'){
   header('Location: childfather.html');
}

答案 1 :(得分:0)

您必须将Post变量与值进行比较:

前两行:

$age = $_POST['age'];
$gender= $_POST['gender'];

你的情况:

  if ($age == "child" && $gender == "male")
  {
       header('Location: childfather.html');
  }

答案 2 :(得分:0)

$gender = $_POST['gender'];

if ($age == "child" && $gender == "male")
{
 header('Location:childfather.html');
}

答案 3 :(得分:0)

您忘了定义$ _POST ['性别']

添加(第3行)

$gender= $_POST['gender'];

然后在您的条件中将运算符更改为==

  if ($age == "child" && $gender == "male")
  {
       header('Location: childfather.html');
  }