当我没有设置单选按钮时,我遇到错误

我尝试做一些条件,当我设置文本字段而不是单选按钮时,我会收到一个php错误。..........................................................................................................................................


这是我的代码:


<form method="post">

   <input type="text" name="name">

   <input type="radio" name="gender" value="female">Female

   <input type="radio" name="gender" value="male">Male

   <input type="radio" name="gender" value="other">Other

   <input type="submit">

</form>


<?php


   $name = $nameErr = $gender = $genderErr = '';


   if ($_SERVER["REQUEST_METHOD"] == "POST") {


       if ($_POST['name'] == '' || $_POST['gender'] == '') {

          echo('all filed');

       }

       else {


          if (empty($_POST["name"])) {

             $nameErr = "Name is required";

          } else {

             $name = $_POST["name"];

          }

          if (empty($_POST["gender"])) {

             $genderErr = "Gender is required";

          } else {

             $gender = $_POST["gender"];

          }


       }


    }


  echo $name. ' ' .$gender;

?>


qq_遁去的一_1
浏览 117回答 4
4回答

动漫人物

试试这个:if ((isset($_POST['name']) && $_POST['name'])&nbsp; == '' || (isset($_POST['gender']) && $_POST['gender'] == '')) {&nbsp; &nbsp; echo('all filed');}你必须把条件,如果设置了,你可以使用调试你的代码:$_POST['gender']echo "<pre>";print_r($_POST);exit;

临摹微笑

尝试使用下面的代码,问题是您尝试打印条件之外的值。if ($_SERVER["REQUEST_METHOD"] == "POST") {&nbsp; &nbsp; if (empty($_POST['name']) || empty($_POST['gender'])) {&nbsp; &nbsp; &nbsp; &nbsp; echo('all filed');&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; if (empty($_POST["name"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $nameErr = "Name is required";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = $_POST["name"];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (empty($_POST["gender"])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $genderErr = "Gender is required";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $gender = $_POST["gender"];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo $name . ' ' . $gender;&nbsp; &nbsp; }}

交互式爱情

根据您的代码,请使用以下条件if (isset($_POST["name"]) && $_POST["name"] != "") {&nbsp; &nbsp; $name = $_POST["name"];&nbsp;&nbsp;} else {&nbsp; &nbsp; $nameErr = "Name is required";}if (isset($_POST["gender"]) && $_POST["gender"] != "") {&nbsp; &nbsp;$gender = $_POST["gender"];&nbsp;&nbsp;} else {&nbsp; &nbsp;$genderErr = "Gender is required";}

饮歌长啸

您在检查“姓名”和“性别”字段时遇到了错误的情况。您已经在条件中给出了“OR(||)”,因此这意味着任何字段都是空的,它将返回true的条件。您的病情是这样的:if ($_POST['name'] == '' || $_POST['gender'] == '') {&nbsp; &nbsp; echo('all filed');}将其更改为 :&nbsp;if ($_POST['name'] == '' && $_POST['gender'] == '') {&nbsp; &nbsp; echo('all filed');&nbsp;}我希望你的问题会得到解决。
打开App,查看更多内容
随时随地看视频慕课网APP