使用PHP发布多个复选框并发送到电子邮件

时间:2014-12-03 21:52:22

标签: php html email checkbox

我使用PHP相当新。我有一个表单,用于将联系表单从网页发送到电子邮件地址。

我无法传递使用逗号设置的多个复选框。例如,如果用户选中checkbox1和checkbox4,代码将被设置样式:checkbox1 ,,, checkbox4。除非用户选中复选框,否则我不想显示逗号。如果这有意义,请告诉我。

这是HTML:

    <code>

<form id="contact_form" class="contact" method="post" action="email_process.php">
            <input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required>
            <input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required><br><br>

            <input class="email text" name="email" type="email" placeholder="EMAIL:" required>
            <input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required><br><br>

            <label>Would you like to be contacted by:</label>
            <input name="how" class="emailbtn radio" type="checkbox" value="Email">
            <label>EMAIL</label>
            <input name="how2" class="phonebtn radio" type="checkbox" value="Phone">
            <label>PHONE NUMBER</label><br><br>

            <div class="one">
            <label class="margin">EVENT DATE:</label><br>
            <input name="month" class="month small" type="number" placeholder="MM">
            <input name="day" class="day small" type="number" placeholder="DD">
            <input name="year" class="year small" type="number" placeholder="YYYY">
            </div>
            <div class="one">
            <label class="margin">EVENT LOCATION:</label><br>
            <input name="location" class="location text" type="text">
            </div>

            <label>Services we may assist you with:</label><br><br>
            <div class="two">
            <input name="services" class="chefbtn radio" type="checkbox" value="Personal Chef">
            <label>PERSONAL CHEF</label><br>
            <input name="services2" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
            <label>PRIVATE COOKING CLASSES</label>
            </div>
            <div class="two">
            <input name="services3" class="chefbtn radio" type="checkbox" value="Event Catering">
            <label>EVENT CATERING</label><br>
            <input name="services4" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
            <label>RESTERAUNT CONSULTING</label>
            </div>

            <input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?">
            <textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"></textarea>

            <input class="submit" type="image" src="../images/contact/s1_submit_btn.png">
        </form>
    </code>

这是我的PHP:

    <code>

<?php

//Prefedined Variables 
$to = "Enter email here";

// Email Subject
$subject = "Contact from your website.";

// This IF condition is for improving security  and Prevent Direct Access to the Mail Script.
if($_POST) {

// Collect POST data from form
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$how = stripslashes($_POST['how']);
$how2 = stripslashes($_POST['how2']);
$phone = stripslashes($_POST['phone']);
$month = stripslashes($_POST['month']);
$day = stripslashes($_POST['day']);
$year = stripslashes($_POST['year']);
$location = stripslashes($_POST['location']);
$services = stripslashes($_POST['services']);
$services2 = stripslashes($_POST['services2']);
$services3 = stripslashes($_POST['services3']);
$services4 = stripslashes($_POST['services4']);
$heard = stripslashes($_POST['heard']);
$comments = stripslashes($_POST['comments']);

if ( ! empty($how) && ! empty($how2) ) { 
$contact_type = $how.', '.$how2;
} elseif (! empty($how)){
    $contact_type = $how;
} elseif (! empty($how2)){
    $contact_type = $how2;
}

if ( ! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) { 
$contact_type2 = $services. ', ' .$services2. ', ' .$services3. ', ' .$services4;
}

// Collecting all content in HTML Table
$content='<table width="100%">
<tr><td  align "center"><b>Contact Details</b></td></tr>
<tr><td>First Name:</td><td> '.$firstname.'</td></tr>
<tr><td>Last Name:</td><td> '.$lastname.'</td></tr>
<tr><td>Email:</td><td> '.$email.' </td></tr>
<tr><td>Phone:</td> <td> '.$phone.'</td></tr>
<tr><td>How Should We Contact You?</td> <td> '.$contact_type.'</td></tr>
<tr><td>Event Date:</td><td> '.$month.' / '.$day.' / '.$year.' </td></tr>
<tr><td>Location:</td> <td> '.$location.'</td></tr>
<tr><td>Which Services Are You Interested In?</td> <td> '.$contact_type2.'</td></tr>
<tr><td>How Did You Hear About Us?</td> <td> '.$heard.'</td></tr>
<tr><td>Comments:</td> <td> '.$comments.'</td></tr>
<tr><td>Date:</td> <td> '.date('d/m/Y').'</td></tr>
</table> ';


// Define email variables
$headers = "From:".$email."\r\n";
$headers .= "Reply-to:".$email."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8';

if (! empty($how) || ! empty($how2)) {
if (! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) {
if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($heard) && ! empty($comments) ) {

// Sending Email 
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
}
}

    </code>

修改

经过一些研究和正确的指导后,我遇到了一个帮助我解决这个问题的答案。如果其他人有同样的问题,请随时参考。

    <?php

//Prefedined Variables 
$to = "Enter email here";

// Email Subject
$subject = "Contact from your website.";

// This IF condition is for improving security  and Prevent Direct Access to the Mail Script.
if($_POST) {

// Collect POST data from form
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$how = 'None';
    if(isset($_POST['how']) && is_array($_POST['how']) && count($_POST['how']) > 0){
    $selectedHow = implode(', ', $_POST['how']);
    }
$phone = stripslashes($_POST['phone']);
$month = stripslashes($_POST['month']);
$day = stripslashes($_POST['day']);
$year = stripslashes($_POST['year']);
$location = stripslashes($_POST['location']);
$services = 'None';
    if(isset($_POST['services']) && is_array($_POST['services']) && count($_POST['services']) > 0){
    $selectedServices = implode(', ', $_POST['services']);
    }
$heard = stripslashes($_POST['heard']);
$comments = stripslashes($_POST['comments']);

// Collecting all content in HTML Table
$content='<table width="100%">
<tr><td  align "center"><b>Contact Details</b></td></tr>
<tr><td>First Name:</td><td> '.$firstname.'</td></tr>
<tr><td>Last Name:</td><td> '.$lastname.'</td></tr>
<tr><td>Email:</td><td> '.$email.' </td></tr>
<tr><td>Phone:</td> <td> '.$phone.'</td></tr>
<tr><td>How Should We Contact You?</td> <td> '.$selectedHow.'</td></tr>
<tr><td>Event Date:</td><td> '.$month.' / '.$day.' / '.$year.' </td></tr>
<tr><td>Location:</td> <td> '.$location.'</td></tr>
<tr><td>Which Services Are You Interested In?</td> <td> '.$selectedServices.'</td></tr>
<tr><td>How Did You Hear About Us?</td> <td> '.$heard.'</td></tr>
<tr><td>Comments:</td> <td> '.$comments.'</td></tr>
<tr><td>Date:</td> <td> '.date('d/m/Y').'</td></tr>
</table> ';


// Define email variables
$headers = "From:".$email."\r\n";
$headers .= "Reply-to:".$email."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8';

if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone)  && ! empty($selectedHow) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($selectedServices) && ! empty($heard) && ! empty($comments) ) {

// Sending Email 
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "Please go back and make sure that all fields have been filled out.";
return false;
}
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}


<form id="contact_form" class="contact" method="post" action="email_process.php">
        <input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required>
        <input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required><br><br>

        <input class="email text" name="email" type="email" placeholder="EMAIL:" required>
        <input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required><br><br>

        <label>Would you like to be contacted by:</label>
        <input name="how[]" class="emailbtn radio" type="checkbox" value="Email">
        <label>EMAIL</label>
        <input name="how[]" class="phonebtn radio" type="checkbox" value="Phone">
        <label>PHONE NUMBER</label><br><br>

        <div class="one">
        <label class="margin">EVENT DATE:</label><br>
        <input name="month" class="month small" type="number" placeholder="MM">
        <input name="day" class="day small" type="number" placeholder="DD">
        <input name="year" class="year small" type="number" placeholder="YYYY">
        </div>
        <div class="one">
        <label class="margin">EVENT LOCATION:</label><br>
        <input name="location" class="location text" type="text">
        </div>

        <label>Services we may assist you with:</label><br><br>
        <div class="two">
        <input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef">
        <label>PERSONAL CHEF</label><br>
        <input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
        <label>PRIVATE COOKING CLASSES</label>
        </div>
        <div class="two">
        <input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering">
        <label>EVENT CATERING</label><br>
        <input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
        <label>RESTERAUNT CONSULTING</label>
        </div>

        <input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?">
        <textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"></textarea>

        <input class="submit" type="image" src="../images/contact/s1_submit_btn.png">
    </form>

2 个答案:

答案 0 :(得分:1)

您需要将这些复选框的name作为数组。像:

<label>Would you like to be contacted by:</label>
<input name="how[]" class="emailbtn radio" type="checkbox" value="Email">
<label>EMAIL</label>
<input name="how[]" class="phonebtn radio" type="checkbox" value="Phone">
<label>PHONE NUMBER</label><br><br>

为服务做同样的事情:

<input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef">
<label>PERSONAL CHEF</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
<label>PRIVATE COOKING CLASSES</label>
</div>
<div class="two">
<input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering">
<label>EVENT CATERING</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">

然后在你的php:

$how = stripslashes($_POST['how']); 
// is actually an array and holds its values in a comma separated format
//no need for how2
//The same approach for services: it's also an array now. if you followed the html above 
$services = stripslashes($_POST['services']);

如果您分配不同的唯一错误消息,那么也不错。

if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone)  && ! empty($how) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($services) && ! empty($heard) && ! empty($comments) ) {

// Sending Email 
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "An error occured and your message could not be sent.";//here 1
return false;
}
}
else {
print "An error occured and your message could not be sent.";//here 2
return false;
}
}

然后,您可以查看是否是失败的外部if( ! empty($firstname) && ...mail功能

答案 1 :(得分:0)

尝试使用php方法数组过滤器和implode:

<?php

$services[] = "test";
$services[] = "";
$services[] = "test";
$services[] = "";

$arr = array_filter($services, function($elem){
    return $elem != "";
});

echo implode(", ", $arr);