dongyue934001 2019-07-23 14:49
浏览 85

如何验证PHP中的复选框? [重复]

I have a problem with validating a checkbox. I want to show error message if "submit" is clicked while checkbox is unchecked. With the code I have right now, form gets submitted anyway. I've tried multiple code snippets I found from other questions but none seem to work..

HTML:

<?php include 'contact-form.php'; ?>

<form id="contact" action="<?php $_SERVER['PHP_SELF']; ?>" method="post" 
enctype="multipart/form-data">
<h3>Add issue</h3>
<fieldset>
  <input placeholder="Nimi" type="text" tabindex="1" name="thename"  value="<?= $thename ?>" autofocus>
    <div class="error"><span><?= $name_error ?></span></div>
</fieldset>
<fieldset>
  <input placeholder="Email" type="text" tabindex="2" name="email" value="<?= $email ?>">
    <div class="error"><span><?= $email_error ?></span></div>
</fieldset>
<fieldset>
<input type="checkbox" name="checkbox1" value="yes"/> Annan ühendusele loa minu eluloo avaldamiseks siinsel kodulehel. <br>
  <div class="error"><span><?= $checkbox1_error ?></span></div>
</fieldset>
</form>

PHP:

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

if (empty($_POST["thename"])) {
    $name_error = "Palun sisesta nimi";
} else {
    $thename = test_input($_POST["thename"]);
    // check if name only contains letters, whitespace and hyphen
    if (!preg_match("/^[a-zA-Z -]*$/",$thename)) {
        $name_error = "Sisestada saab ainult tähti, tühikuid ja sidekriipse";
    }
}

if (empty($_POST["email"])) {
  $email_error = "Palun sisesta email";
} else {
    $email = test_input($_POST["email"]);
    // email validation
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Sisesta email korrektselt";
    }
}    

if(!isset($_POST['checkbox1'])) {
  //checkbox is not checked
  $checkbox1_error = 'Väli peab olema kinnitatud';
}

if ($name_error == '' and $email_error == '' and $message_error == '' and $attachment_error == '' and $checkbox1_error == ''){
    $message_body = '';
    unset($_POST['submit']);
    foreach ($_POST as $key => $value){
        $message_body .= "$key: $value
";
    }


    $to = 'myemail@gmail.com';
    $subject = 'My subject';
    $message = "My message";

if (@mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=', $datamsg, $headers, "-f$email")){
        $success = "Aitäh, kiri edukalt saadetud!";
    } else {
        $error = "Saatmine ebaõnnestus. Palun proovige uuesti.";
    }

Any help is appreciated!

</div>
  • 写回答

1条回答 默认 最新

  • dongsuoying9059 2019-07-23 15:03
    关注

    I used this code and its showing the error message for checkboxThe Image with Checkbox error

    <?php
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
        if (empty($_POST["thename"])) {
            $name_error = "Palun sisesta nimi";
        } else {
            $thename = $_POST["thename"];
            // check if name only contains letters, whitespace and hyphen
            if (!preg_match("/^[a-zA-Z -]*$/", $thename)) {
                $name_error = "Sisestada saab ainult tähti, tühikuid ja sidekriipse";
            }
        }
    
        if (empty($_POST["email"])) {
            $email_error = "Palun sisesta email";
        } else {
            $email = $_POST["email"];
            // email validation
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $email_error = "Sisesta email korrektselt";
            }
        }
    
        if (!isset($_POST['checkbox1'])) {
            //checkbox is not checked
            $checkbox1_error = 'Väli peab olema kinnitatud';
        }
    
        if (!isset($name_error) and ! isset($email_error) and ! isset($message_error) and ! isset($attachment_error) and ! isset($checkbox1_error)) {
            $message_body = '';
            unset($_POST['submit']);
            foreach ($_POST as $key => $value) {
                $message_body .= "$key: $value
    ";
            }
    
    
            $to = 'myemail@gmail.com';
            $subject = 'My subject';
            $message = "My message";
    
            if (@mail($to, '=?UTF-8?B?' . base64_encode($subject) . '?=', $datamsg, $headers, "-f$email")) {
                $success = "Aitäh, kiri edukalt saadetud!";
            } else {
                $error = "Saatmine ebaõnnestus. Palun proovige uuesti.";
            }
        }
    }
    ?>
    <form id="contact" action="<?php $_SERVER['PHP_SELF']; ?>" method="post" 
          enctype="multipart/form-data">
        <h3>Add issue</h3>
        <fieldset>
            <input placeholder="Nimi" type="text" tabindex="1" name="thename"  value="<?php echo $_POST['thename'] ?>" autofocus>
            <div class="error"><span><?php echo $name_error ?></span></div>
        </fieldset>
        <fieldset>
            <input placeholder="Email" type="text" tabindex="2" name="email" value="<?php echo $_POST['email'] ?>">
            <div class="error"><span><?php echo $email_error ?></span></div>
        </fieldset>
        <fieldset>
            <input type="checkbox" name="checkbox1" value="yes"/> Annan ühendusele loa minu eluloo avaldamiseks siinsel kodulehel. <br>
            <div class="error"><span><?php echo $checkbox1_error ?></span></div>
        </fieldset>
        <input type="submit" value="Submit"/>
    </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。