簡體   English   中英

如何在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."; 
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM