2单选按钮选项的条件

时间:2018-04-23 09:17:14

标签: php html checkbox radio-button

我是php语言的新手。目前我正在制作一个表单,让用户点击提交,输出将发送到我的电子邮件(使用phpmailer)。

现在所有工作正常,但我只是想做一个小调整,其中如果用户对单选按钮回答YES,那么用户需要检查至少1个复选框才能处理表单,而如果用户为单选按钮回答NO,则如果他们不选中复选框则不会显示错误。需要了解如何调整代码以使其像这样。

表单HTML

<div class="form-group">
        <p>Have you worn contact Lens before?</p>
        <input type="radio" name="lenses"  value="Yes"/> Yes<br>
        <input type="radio" name="lenses"  value="No"/> No<br>
</div>
<div class="form-group">
        <p>If yes, which type of disposable contact lens have you worn before?</p>
        <input type="checkbox" name="disposable_lens[]"  value="Monthly"/> Monthly<br>
        <input type="checkbox" name="disposable_lens[]"  value="Daily Disposables"/> Daily Disposables<br>
</div>

PHP

if(isset($_POST["submit"]))
{
if(empty($_POST["lenses"]))
{
    $error .= '<p><label class="text-danger">Lenses is required</label></p>';
}
else
{
    $lenses = clean_text($_POST["lenses"]);
}
if(empty($_POST["disposable_lens"]))
{
    $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
}
else
{
    $disposable_lens = array_map('clean_text', $_POST["disposable_lens"]);

这是phpmailer正文消息,如果你们想看到

$mail->Body = join('', array(
        "Outlet: ",
        $_POST['outlet'],
        "<br/>",
        "Full Name: ",
        $_POST['fullname'],
        "<br/>",
        "Email Address: ",
        $_POST['email'],
        "<br/>",
        "Phone Number:",
        $_POST['contact_no'],
        "<br/>",
        "Have you worn contact lens before?: ",
        $_POST['lenses'],
        "<br/>",
        "If yes, which type of disposable contact lens have you worn before?: ",
        join(', ', $_POST['disposable_lens']),
        "<br/>",
        "Appointment Date ",
        $_POST['date'],
        "<br/>",
        "Appointment Time ",
        $_POST['time']
        ));                                     

2 个答案:

答案 0 :(得分:1)

你可以在php中做这样的事情......你可以在评论中添加一些javascript作为@Rameleu sugested。这将改善用户体验。

if(isset($_POST["submit"])) {
    // Check if lenses is yes or no (not emty)
    if(empty($_POST["lenses"])) {
        $error .= '<p><label class="text-danger">Lenses is required</label></p>';
    }
    else {
        // When lenses is yes and no disposable lens is checked
        if($_POST["lenses"] == "yes" && count($_POST["disposable_lens"]) == 0) {
            $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
        }
        // When lenses is yes and one (or more) disposable lens is checked
        elseif($_POST["lenses"] == "yes" && count($_POST["disposable_lens"]) > 0) {
            $lenses = clean_text($_POST["lenses"]);
            $disposable_lens = array_map('clean_text', $_POST["disposable_lens"]);
        }
        // When lenses is no
        elseif($_POST["lenses"] == "no") {
            $lenses = clean_text($_POST["lenses"]);

            // Can they still use the checkboxes? Then you'ld need this
            if(count($_POST["disposable_lens"]) > 0) {
                $disposable_lens = array_map('clean_text', $_POST["disposable_lens"]);
            }
        }
    }
}

答案 1 :(得分:0)

&#13;
&#13;
function show() {	
    var x = document.getElementById("myDIV");    
        x.style.display = "block";   
}

function hide(){
 var x = document.getElementById("myDIV");    
        x.style.display = "none";   
}
&#13;
#myDIV { display:none; }
&#13;
<form action="" method="POST">
<div class="form-group">
        <p>Have you worn contact Lens before?</p>
        <input onclick="show()" type="radio" name="lenses"  value="Yes"/> Yes<br>
        <input onclick="hide()"type="radio" name="lenses"  value="No"/> No<br>
</div>
<div class="form-group" id="myDIV">
        <p>If yes, which type of disposable contact lens have you worn before?</p>
        <input  type="checkbox" name="disposable_lens[]"  value="Monthly"/> Monthly<br>
        <input  type="checkbox" name="disposable_lens[]"  value="Daily Disposables"/> Daily Disposables<br>
</div>
<?php echo $error; ?>
<input type="submit" name="submit" value="submit">
</form>
&#13;
&#13;
&#13;

    if(isset($_POST["submit"])) {
if(empty($_POST["lenses"])) {
    $error .= '<p><label class="text-danger">Lenses is required</label></p>';
}

else {
    if($_POST["lenses"] == "Yes" && empty($_POST["disposable_lens"])) {
        $error .= '<p><label class="text-danger">Disposable_lens is required</label></p>';
    }
    else{
        $disposable_lens[] =  $_POST["disposable_lens"];
    }   
}

if(!empty($_POST["disposable_lens"])) {
    $disposablelens = implode(",",$disposable_lens[0]);
}
    if(empty($error)) {
        echo "Have you worn contact Lens before? ".$_POST["lenses"]."<br>";
        echo "If yes, which type of disposable contact lens have you worn before? ".$disposablelens;
    }
}