繁体   English   中英

PHP验证复选框

[英]PHP validation checkbox

我搜寻了整个互联网,试图找到解决方案(我做错了)。 即使选中该复选框,我的表单也无法验证。 其他一切正常。

这是我无法正常使用的复选框。

我尝试了许多不同的想法,但是即使选中了“条件”,它也无法验证(例如下面的示例)。

这是我的HTML:

<div class="form-check">
      <input type="checkbox" class="form-check-input" name="terms" id="terms"  />
     <label class="form-check-label" for="terms"><p>I agree to terms of service.</p></label>
</div>

这是我的整个PHP验证(使用下面的评论/答案更新):

<?php

if(!$_POST) exit;

// Email address verification.
function isEmail($email) { // lots of email validation stuff in here // }

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name     = $_POST['name'];
$email    = $_POST['email'];
$website   = $_POST['website'];
$subject  = $_POST['subject'];
$comments = $_POST['comments'];
$terms = $_POST['terms'];


//set an error counter to trigger the `exit`
$error_counter = 0;
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name</div>';
$error_counter++;
} 
if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email 
address.</div>';
$error_counter++;
}
if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have entered an invalid e- 
mail address, try again.</div>';
$error_counter++;
} 
if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
$error_counter++;
}
if(trim($comments) == '') {
echo '<div class="error_message">Attention! Please enter your message. 
</div>';
$error_counter++;
}
if(empty($terms)) {
echo '<div class="error_message">Attention! Please agree to our terms of 
service.</div>';
$error_counter++;
}
//if `$error_counter > 0 > 0` it will trigger the `exit()` to stop the script and display the errors.
if($error_counter > 0){
exit();
}

if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}


// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe@yourdomain.com";

$address = "cousinjack@mydomain.com";


// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."

// Example, $e_subject = '$name . ' has contacted you via Your Website.';

$e_subject = 'You have been contacted by ' . $name . '.';


// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

$e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email ";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

// Email has sent successfully, echo a success page.

echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";

} else {

echo 'ERROR!';

}

?>

您没有为复选框设置任何value属性:

<input type="checkbox" class="form-check-input" name="terms" id="terms" value="yes" />

更多信息: https : //developer.mozilla.org/fr/docs/Web/HTML/Element/Input/checkbox

我可以看到您的复选框没有任何值,因此PHP部分正在获得垃圾/空值。

简而言之,只需在HTML端添加一个required属性即可。 无需在服务器端进行检查

<div class="form-check">
      <input type="checkbox" class="form-check-input" name="terms" id="terms" required/>
     <label class="form-check-label" for="terms"><p>I agree to terms of service.</p></label>
</div>

当前验证的问题在于,您错误地使用了if-else并且对每条语句都会使用exit ,这会过早地终止脚本。

您可以改为:

For Terms:
<input type="checkbox" class="form-check-input" name="terms" id="terms" value="1"/>    

//set an error counter to trigger the `exit`
$error_counter = 0;
if(trim($name) == '') {
    echo '<div class="error_message">Attention! You must enter your name</div>';
    $error_counter++;
} 
if(trim($email) == '') {
    echo '<div class="error_message">Attention! Please enter a valid email 
address.</div>';
    $error_counter++;
}
if(!isEmail($email)) {
    echo '<div class="error_message">Attention! You have entered an invalid e- 
mail address, try again.</div>';
    $error_counter++;
} 
if(trim($subject) == '') {
    echo '<div class="error_message">Attention! Please enter a subject.</div>';
    $error_counter++;
}
if(trim($comments) == '') {
    echo '<div class="error_message">Attention! Please enter your message.</div>';
    $error_counter++;
}
if(empty($terms)) {
    echo '<div class="error_message">Attention! Please agree to our terms of service.</div>';
    $error_counter++;
}
//if `$error_counter > 0 > 0` it will trigger the `exit()` to stop the script and display the errors.
if($error_counter > 0){
    exit();
}

有一种更漂亮的方法可以做到这一点。 随时为OP添加建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM