dongli564510 2014-12-03 21:52
浏览 54
已采纳

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

I am fairly new with using PHP. I have a form that I am using to send a contact form form a webpage to an email address.

I am having trouble passing multiple checkboxes styled with a comma. For example, if the user picks checkbox1 and checkbox4, the code will be styled: checkbox1, , , checkbox4. I don't want to display the commas, unless the user picks the checkboxes. Let me know if that makes sense or not.

This is the 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>

This is my 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."
";
$headers .= "Reply-to:".$email."
";
$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>

EDIT:

After some research and the right guidance I came across an answer that helped me solve this. If anyone else is having the same problem feel free to refer to this.

    <?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."
";
$headers .= "Reply-to:".$email."
";
$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条回答 默认 最新

  • duanjing7459 2014-12-03 22:07
    关注

    You need to have the name of those checkboxes as an array. Like:

    <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>
    

    Do same for services:

    <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">
    

    Then in your 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']);
    

    It will also be good if you assign different unique error messages unlike you did along

    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;
    }
    }
    

    You can then find out if it's the outer if( ! empty($firstname) && ... or the mail function that is failing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog