【问题标题】:How to insert check boxes and radio buttons in php contact form如何在 php 联系表单中插入复选框和单选按钮
【发布时间】:2017-05-01 19:26:22
【问题描述】:

我想添加周一至周六的复选框和 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 新手。

所有帮助将不胜感激。

【问题讨论】:

    标签: php html forms contact


    【解决方案1】:

    只需将 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."; 
    }
    ?>
    

    【讨论】:

    • 我试试看非常感谢您的反馈,在游戏中还是新的。
    • 如果您觉得我的回答有帮助,请接受我的回答或投票。如果你是新手,最好去看看一些教程,例如w3schools
    • 您好 Mat 它不起作用,因为我收到一个错误:此页面不起作用 www.website-name.co.za 目前无法处理此请求。 HTTP 错误 500
    • 它不发送电子邮件,但是当我取出这个时 if(!empty($_POST['days'])) { $selected_days = $_POST['days'] } $radio_selected_option = $_POST['my_radio]; - 然后它发送
    • @Jonny-JohnathanStockenstroom 我的错,我有错字。我在我的电脑上进行了修复和测试,所以现在它应该可以工作了。
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多