如何在 php 联系表单中插入复选框和单选按钮

How to insert check boxes and radio buttons in php contact form

我想添加周一至周六的复选框和 2 个单选按钮。你能帮助我或给我一些指导吗?这是我的 html 和 php 发送脚本。

HTML:

<form class="contact-form row" name="commentform" method="post" action="send_form_email.php">
    <div class="col-md-4">
        <label></label>
        <input type="text" class="form-control" required="required" id="name" name="name" placeholder="Name">
    </div>
    <div class="col-md-4">
        <label></label>
        <input type="text" class="form-control" required="required" id="email" name="email" placeholder="Email">
    </div>
    <div class="col-md-4">
        <label></label>
        <input type="text" class="form-control" required="required" id="phone" name="phone" placeholder="Phone">
    </div>
    <div class="col-md-12">
        <label></label>
        <textarea class="form-control" rows="9" required="required" id="message" name="message" placeholder="Your message here.."></textarea>
    </div>
    <div class="col-md-4 col-md-offset-4">
        <label></label>
        <button type="submit" data-toggle="modal" data-target="#alertModal" class="btn btn-primary btn-block btn-lg">Send <i class="ion-android-arrow-forward"></i></button>
    </div>
</form>

PHP 脚本:

<?php
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "";
$email_subject = "";
$stop_reason = '';
if(!ISSET($_POST['email'])) {
    echo("This page can only be accessed from the contact form.  Sorry about that.<br />");
    exit;
}
// validation expected data exists
if((!ISSET($_POST['name']) OR $_POST['name'] == '') OR
(!ISSET($_POST['phone']) OR $_POST['phone'] == '') OR
(!ISSET($_POST['email']) OR $_POST['email'] == '') OR
(!ISSET($_POST['message']) OR $_POST['name'] == '')) {
    echo("All of the form fields are required.<br />");
    exit;
}
$name = $_POST['name']; // required
$phone = $_POST['phone']; // required
$email_from = $_POST['email']; // required    
$message = $_POST['message']; // required
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)){
    $stop = '1';
    $stop_reason = $stop_reason."Email address does not appear to be valid.<br />";
}
$phone_exp = '/^[0-9]*$/';
if(!preg_match($phone_exp,$phone)) {
    $stop = '1';
    $stop_reason = $stop_reason.'The phone number you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
    $stop = '1';
    $stop_reason = $stop_reason.'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
    $stop = '1';
    $stop_reason = $stop_reason.'The Message you entered do not appear to be valid.<br />';
}
if(ISSET($stop)){
    echo("
    Your message was unable to be sent because of the following:<br />
    <br />
    ".$stop_reason."
    <br />
    Please feel free to try again.
    ");
    UNSET($stop);
    exit;
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";   
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($email_to,$email_subject,$email_message,$headers)) { 
     echo '<script type="text/javascript">alert("Thank you, your request has been submitted!");</script>';
    echo "<meta http-equiv='refresh' content='0;url='>";
}else{ 
     echo "There was a problem sending the mail.  We're really sorry about that."; 
}
?>

因此,如果有人可以在正确的方向上帮助我或帮助我,我对 PHP 还是个新手。

我们将不胜感激。

只需将 html 个元素添加到表单中即可:

<form class="contact-form row" name="commentform" method="post" action="send_form_email.php">
                <div class="col-md-4">
                    <label></label>
                    <input type="text" class="form-control" required="required" id="name" name="name" placeholder="Name">
                </div>
                <div class="col-md-4">
                    <label></label>
                    <input type="text" class="form-control" required="required" id="email" name="email" placeholder="Email">
                </div>
                <div class="col-md-4">
                    <label></label>
                    <input type="text" class="form-control" required="required" id="phone" name="phone" placeholder="Phone">
                </div>
                <div class="col-md-12">
                    <label></label>
                    <textarea class="form-control" rows="9" required="required" id="message" name="message" placeholder="Your message here.."></textarea>
                </div>
                <input type="checkbox" name="days[]" value="Monday">Monday<br>
                <input type="checkbox" name="days[]" value="Tuesday">Tuesday<br>
                <input type="checkbox" name="days[]" value="Wednesday">Wednesday<br>
                <input type="checkbox" name="days[]" value="Thursday">Thursday<br>
                <input type="checkbox" name="days[]" value="Friday">Friday<br>
                <input type="checkbox" name="days[]" value="Saturday">Saturday<br>

                <input type="radio" name="my_radio" value="option_1">option 1<br>
                <input type="radio" name="my_radio" value="option_1">option 2<br>
                <div class="col-md-4 col-md-offset-4">
                    <label></label>
                    <button type="submit" data-toggle="modal" data-target="#alertModal" class="btn btn-primary btn-block btn-lg">Send <i class="ion-android-arrow-forward"></i></button>
                </div>
            </form>

然后您可以在 PHP 脚本中获取它们

<?php
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "";
$email_subject = "";
$stop_reason = '';
if(!ISSET($_POST['email'])) {
    echo("This page can only be accessed from the contact form.  Sorry about that.<br />");
    exit;
}
// validation expected data exists
if((!ISSET($_POST['name']) OR $_POST['name'] == '') OR
(!ISSET($_POST['phone']) OR $_POST['phone'] == '') OR
(!ISSET($_POST['email']) OR $_POST['email'] == '') OR
(!ISSET($_POST['message']) OR $_POST['name'] == '')) {
    echo("All of the form fields are required.<br />");
    exit;
}
$name = $_POST['name']; // required
$phone = $_POST['phone']; // required
$email_from = $_POST['email']; // required    
$message = $_POST['message']; // required

if(!empty($_POST['days'])) {
    $selected_days = $_POST['days'];
}
$radio_selected_option = $_POST['my_radio'];

if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)){
    $stop = '1';
    $stop_reason = $stop_reason."Email address does not appear to be valid.<br />";
}
$phone_exp = '/^[0-9]*$/';
if(!preg_match($phone_exp,$phone)) {
    $stop = '1';
    $stop_reason = $stop_reason.'The phone number you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
    $stop = '1';
    $stop_reason = $stop_reason.'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
    $stop = '1';
    $stop_reason = $stop_reason.'The Message you entered do not appear to be valid.<br />';
}
if(ISSET($stop)){
    echo("
    Your message was unable to be sent because of the following:<br />
    <br />
    ".$stop_reason."
    <br />
    Please feel free to try again.
    ");
    UNSET($stop);
    exit;
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";   
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($email_to,$email_subject,$email_message,$headers)) { 
 echo '<script type="text/javascript">alert("Thank you, your request has 
been submitted!");</script>';
    echo "<meta http-equiv='refresh' content='0;url='>";
}else{ 
    echo "There was a problem sending the mail.  We're really sorry about that."; 
}
?>