如何检查选中的单选按钮

时间:2014-06-15 18:18:26

标签: php html

我想在PHP中使用一个可以检查选中哪个单选按钮的脚本。我有2个单选按钮(例如rad1rad2)。当用户点击一个按钮并提交时,我想从该单选按钮回显该值。

我找到了一个例子,但非常糟糕:example

我已经为电子邮件创建了这个并传递:

$email=$_POST['email'];
$password= $_POST['password'];

if (isset($email)  && !empty($email) ) {
    echo $email;

    if (isset($password) && !empty($password)) {
        echo $password;
    }
}

表格:

<form action="" method="POST">
    <input type="text" name="email" placeholder="email"><br/>

    <input type="password" name="password" placeholder="password"><br/>

    <input type="radio" name="sex" value="student">Student<br>
    <input type="radio" name="sex" value="profesor">Profesor<br>
    <input type="Submit" value="Submit">
</form>

如何查看已选中哪个单选按钮?

2 个答案:

答案 0 :(得分:1)

<form action="" method="POST">
    <input type="text" name="email" placeholder="email"><br/>

    <input type="password" name="password" placeholder="password"><br/>

    <input type="radio" name="sex" value="student">Student<br>
    <input type="radio" name="sex" value="profesor">Profesor<br>
    <input type="Submit" value="Submit">
</form>

<?php

$sex = isset($_POST['sex']) ? trim($_POST['sex']) : null;
switch($sex) {
    case 'student' :

        break;

    case 'profesor' :

        break;

    default :

        break;
}

答案 1 :(得分:0)

<form method="post">
<input type="radio" name="rad1" value="1">
<input type="radio" name="rad2" value="1">
<button type="submit">SEND</button>
</form>

<?php

$rad1 = isset($_POST['rad1'])?trim($_POST['rad1']):null;
$rad2 = isset($_POST['rad2'])?trim($_POST['rad2']):null;
switch($rad1) {
    case 1 :
        // do something
      break;

    default: 
       // do something if null
      break;
}