【问题标题】:Posting multiple checkboxes using PHP and sending to an email使用 PHP 发布多个复选框并发送到电子邮件
【发布时间】:2015-02-01 15:29:16
【问题描述】:

我对使用 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>

【问题讨论】:

    标签: php html email checkbox


    【解决方案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) &amp;&amp; ... 还是 mail 函数

    【讨论】:

    • 我按照你说的做了,现在我收到 500 错误,并且字段根本没有被转移。
    • 请查看编辑,查看编辑后的代码,如果我做错了什么,请告诉我。
    • 检查你的 apache 错误日志是否有任何与服务器相关的错误,因为这很可能是一个 php 错误。
    • 它发布有错误并且不发送电子邮件。这可能是我检查字段不为空的方式吗?
    • 什么特别的错误?服务器错误还是您的user-defined 错误?
    【解决方案2】:

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

    <?php
    
    $services[] = "test";
    $services[] = "";
    $services[] = "test";
    $services[] = "";
    
    $arr = array_filter($services, function($elem){
        return $elem != "";
    });
    
    echo implode(", ", $arr);
    

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 1970-01-01
      • 2019-05-02
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      相关资源
      最近更新 更多