单选按钮仅返回" on"提交时

时间:2018-03-07 02:53:21

标签: php post button form-submit radio

我是PHP的新手,我试图在字符串中显示所选单选按钮的值。但我得到的只是" on"而不是与选择相关的值。任何帮助都会很棒!

<form action="Handle_form_PHP.php" method="post">
    <div class= "row">
      <div class ="col-25">
        <label for="student">Student</label>
      </div>
      <div class ="col-75">
        <input type="radio" name="student" value ="student">
      </div>
    </div>

    <div class= "row">
      <div class ="col-25">
        <label for="student">Employee</label>
      </div>
      <div class ="col-75">
        <input type="radio" name="student" value = "employee">
      </div>
    </div>

    <div class="row">
      <input type="submit" value="Submit">
    </div>
</form>

这是我用来打印的PHP:

// Create a shorthand for the form data:
$fname = $_REQUEST['firstname'];
$lname = $_REQUEST['lastname'];
$address = $_REQUEST['address'];
$state1 = $_REQUEST['state1'];
$state2 = $_REQUEST['state2'];
$student = $_POST['student'];

// Print the submitted information:
echo "<p>Thank you, <b> $student </b>, <b>$fname $lname</b>, for the filling out our survey:<br>
    <p>We will Send you your report at:  <i>$address</i>, <i>$state1</i><i>$state2</i>.</p>\n";

2 个答案:

答案 0 :(得分:0)

如果您的$_POST['student']超全局变量的值为&#39;,那么该问题与方案区分大小写无关。

单选按钮的默认值为&#39; on&#39;除非您在单选按钮标记内提供value属性。

让我向你证明。转到[cringe] https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox,然后将以下代码段粘贴到左侧框中,然后运行,然后提交。

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php" method="get">
  <input type="radio" name="student" checked> Student<br>
  <input type="submit" value="Submit">
</form>
</body>
</html>

这是使用get方法提交的(请注意小写get无关紧要。post的行为方式相同。)如果学生电台输入中没有value,则提交的值为on

如果您在收音机输入中写下value="Red42",您将不再收到on;提交的值为Red42

答案 1 :(得分:-2)

在说方法时使用POST而不是帖子:

<form action="Handle_form_PHP.php" method="POST">

<form action="Handle_form_PHP.php" method="post">

为了记录,这本书不正确并且发布了#34;