如何为PHP联系表单添加复选框强制勾选验证
Let's walk through adding the checkbox and corresponding validation to your existing form. I'll break this into frontend and backend changes so it's easy to follow.
Frontend: Add the Checkbox to Your Form
Insert this form group right before the submit button's group in your HTML. This adds a required checkbox with a clear, user-friendly label:
<div class="form-group"> <div class="col-md-12"> <label class="checkbox-inline"> <input type="checkbox" id="agree" name="agree" value="1" required> * I agree to the terms and conditions </label> </div> </div>
The required attribute triggers basic HTML5 validation in the browser, but we'll still add backend validation to ensure users can't bypass this check.
Backend: Add Validation Logic in PHP
Now update your PHP code to handle the checkbox validation properly:
Step 1: Include the Checkbox Value in $send_data
Add this line to your $send_data array setup (place it alongside the other field definitions):
$send_data['agree'] = (isset($_POST['agree']) && $_POST['agree'] == '1') ? '1' : '';
Step 2: Add the Validation Check
Insert this validation block after your existing field checks (e.g., right after the message validation):
# Validate the agreement checkbox if ($send_data['agree'] != '1') { $errors[] = 'Please agree to the terms and conditions to submit your enquiry'; }
Updated Full Backend Snippet (Relevant Sections)
Here's how your modified PHP code will look for the key parts (I also fixed a small bug in your email message loop that wasn't displaying field values):
<?php $target_page = false; $send_to = config('company_email'); $company_name = config('company_name'); $errors = array(); $posted = false; $success = false; $current_url = (isset($_POST['page']) && !empty($_POST['page'])) ? $_POST['page'] : 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; # is this the normal or quick form? $quick = (isset($_POST['type']) && $_POST['type'] == 'quick') ? true : false; # set up the common fields $send_data = array(); $send_data['name'] = (isset($_POST['name']) && !empty($_POST['name'])) ? $_POST['name'] : ''; $send_data['email'] = (isset($_POST['email']) && !empty($_POST['email'])) ? $_POST['email'] : ''; # if this is not the quick form then add the additional fields if (!$quick) { $send_data['tel'] = (isset($_POST['tel']) && !empty($_POST['tel'])) ? $_POST['tel'] : ''; } $send_data['message'] = (isset($_POST['message']) && !empty($_POST['message']) && $_POST['message'] != ' ') ? $_POST['message'] : ''; // Add checkbox value to send_data $send_data['agree'] = (isset($_POST['agree']) && $_POST['agree'] == '1') ? '1' : ''; # check if the form has been submitted if (isset($_POST['bts']) && $_POST['bts'] == '') { # the form has been posted $posted = true; # check the name value if ($send_data['name'] == '') { $errors[] = 'Please fill in your name'; } # check the email value if ($send_data['email'] == '') { $errors[] = 'Please fill in your email address'; } else { # validate the email $validate_email = filter_var($send_data['email'], FILTER_VALIDATE_EMAIL); if (!$validate_email) { $errors[] = 'Please check your email address is correct'; } else { # check the domains MX records if(!checkdnsrr(array_pop(explode("@",$send_data['email'])),"MX")){ $errors[] = 'Please use a valid email address'; } } } # if this is not the quick form then check the additional fields if (!$quick) { if (strlen($send_data['tel']) < 10) { $errors[] = 'Please add a valid telephone number!'; } } # check the message if ($send_data['message'] == '') { $errors[] = 'Please fill in a message'; } # Validate checkbox agreement if ($send_data['agree'] != '1') { $errors[] = 'Please agree to the terms and conditions to submit your enquiry'; } # if there are no errors then send the message if (count($errors) == 0) { $send_from = $send_data['name'] . "<" . $send_data['email'] . ">"; $subject = 'Enquiry From Website ' . $_SERVER['HTTP_HOST']; $email_message = 'Below are the details that have been submitted on your contact form' . "\n\n"; $email_message .= '________________________________________' . "\n\n"; if (count($send_data) > 0) { foreach ($send_data as $key => $value) { $email_message .= ucfirst($key) . ": " . $value . "\n"; // Fixed to display field keys and values } } // Don't forget to add your mail() function call here if missing! // Example: mail($send_to, $subject, $email_message, "From: $send_from"); $success = true; } } ?>
Final Notes
- The HTML5
requiredattribute gives immediate frontend feedback, but backend validation is non-negotiable — users can easily bypass frontend checks. - You can customize the label text to match your actual requirements (e.g., link to a terms page if needed).
内容的提问来源于stack exchange,提问作者user3025532

