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 Mac虚拟机安装Windows,Windows中用idea跑项目,报错process finished with exit code -1073741819(0xc0000005) 如何解决?
  • ¥15 关于python爬虫返回js数据结构的处理
  • ¥15 ubuntu所用stm32标准库开发烧录没反映
  • ¥15 运维工程师(在职人员回答)
  • ¥15 Kmeans的趋势分析有会的吗
  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关