简体   繁体   English

没有从复选框数组中获取值

[英]Not getting values from an array of checkboxes

In this form I'm building I'm having an issue getting the array to list in the email that is received by the site owner upon submission of the form. 在此表单中,我正在构建一个问题,无法在提交表单时将数组列在网站所有者收到的电子邮件中。 Here is the how the received email looks: 这是收到的电子邮件的外观:

Company: Company Name 公司:公司名称
First Name: Bob 名:鲍勃
Last Name: Jones 姓氏:Jones
E-mail: email@gmail.com 电子邮件:email@gmail.com
Phone: 123-652-9658 电话:123-652-9658
Street Address: 123 Main Street 街道地址:大街123号
City: Albany 城市:奥尔巴尼
State: NY 州:纽约
Zip: 12345 邮编:12345
Areas of Interest: 兴趣范围:
Number of windows / Doors: 6 门窗数量:6
How did you hear about our company? 您是如何得知我们的公司的? google bing 谷歌兵
Additional Information: hi 附加信息:嗨

Here is the form code: 这是表单代码:

<form method="post" action="sendeform.php">
         <p><label for="company">Company:</label> <input type="text" class="form-control" name="company" id="company" tabindex="1" /></p>
        <p><label for="firstname">First Name:*</label> <input type="text" class="form-control" name="firstname" id="firstname" tabindex="2" /></p>
        <p><label for="lastname">Last Name:*</label> <input type="text" class="form-control" name="lastname" id="lastname" tabindex="3" /></p>
        <p><label for="email">Email:*</label> <input type="text" class="form-control" name="email" id="email" tabindex="4"  /></p>
        <p><label for="phone">Phone:*</label> <input type="text" class="form-control" name="phone" id="phone" tabindex="5" /></p>
        <p><label for="street">Street Address:*</label> <input type="text" class="form-control" name="street" id="street" tabindex="6" /></p>
        <p><label for="city">City:*</label> <input type="text" class="form-control" name="city" id="city" tabindex="6" /></p>
        <p><label for="state">State:*</label> <input type="text" class="form-control" name="state" id="state" tabindex="6" /></p>
        <p><label for="zip">Zip:*</label> <input type="text" class="form-control" name="zip" id="zip" tabindex="6"  /></p>
        <p><label for="interest">Areas of Interest:*</label><br>

          <input type="checkbox" name="check_list[]" value="Heat Reduction" /> Heat Reduction<br>
            <input type="checkbox" name="check_list[]" value="UV/Fading" /> UV/Fading<br>
            <input type="checkbox" name="check_list[]" value="Wall Finishes" /> Wall Finishes<br>
            <input type="checkbox" name="check_list[]" value="Sun Control" /> Sun Control<br>
            <input type="checkbox" name="check_list[]" value="Safety/Security Film" /> Safety/Security Film<br>
            <input type="checkbox" name="check_list[]" value="Graphics" /> Graphics<br>
             <input type="checkbox" name="check_list[]" value="Decorative Film" /> Decorative Film<br>
            <input type="checkbox" name="check_list[]" value="Wall Murals" /> Wall Murals<br></p>
            <p><label for="doors">Number of windows / Doors:</label> <input type="text" class="form-control" name="doors" id="doors" tabindex="6" /></p>

 <p><label for="hear">How did you hear about our company?</label> <textarea  class="form-control" name="hear" id="hear" cols="12" rows="6" tabindex="7"></textarea></p>           

        <p><label for="info">Additional Information:</label> <textarea class="form-control" name="info" id="info" cols="12" rows="6" tabindex="8"></textarea></p>

        <p><input name="estimatesubmit" type="submit" id="estimatesubmit" class="submit" value="Send" tabindex="9" /></p>

</form>

Here is the PHP to run the form: 这是运行表格的PHP:

<?php


if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}}}




$company = filter_var($_POST['company'], FILTER_SANITIZE_STRING);
$firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
$lastname = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$street = filter_var($_POST['street'], FILTER_SANITIZE_STRING);
$city = filter_var($_POST['city'], FILTER_SANITIZE_STRING);
$state = filter_var($_POST['state'], FILTER_SANITIZE_STRING);
$zip = filter_var($_POST['zip'], FILTER_SANITIZE_STRING);
$doors = filter_var($_POST['doors'], FILTER_SANITIZE_STRING);
$hear = filter_var($_POST['hear'], FILTER_SANITIZE_STRING);
$info = filter_var($_POST['info'], FILTER_SANITIZE_STRING);

$site_owners_email = 'email@gmail.com'; // Replace this with your own email address
$site_owners_name = 'Client Name'; // replace with your name
$site_owners_name_from = 'Free Estimate Submission';

if (strlen($firstname) < 2) {
    $error['firstname'] = "Please enter your first name";
}

if (strlen($lastname) < 2) {
    $error['lastname'] = "Please enter your last name";
}

if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address";
}

if (strlen($phone) < 3) {
    $error['phone'] = "Please enter your phone number.";
}

if (!$error) {

    require_once('phpMailer/class.phpmailer.php');
    $mail = new PHPMailer();

    $mail->From = $email;
    $mail->FromName = $site_owners_name_from;
    $mail->Subject = "Form Submission";
    $mail->AddAddress($site_owners_email, $site_owners_name);
    $mail->IsHTML(true);
    $mail->Body = 'The estimate form on your website, 3Msecurityfilm.com, has been filled out.'. '<br/><br/>'. '<b>Company:</b> '. $company . '<br/><b>First Name:</b> '. $firstname . '<br/><b>Last Name:</b> '. $lastname .'<br/><b>E-mail:</b> '. $email .'<br/><b>Phone:</b> '. $phone .'<br/><b>Street Address:</b> '. $street .'<br/><b>City:</b> '. $city . '<br/><b>State:</b> '. $state . '<br/><b>Zip:</b> '. $zip . '<br/><b>Areas of Interest:</b> '. $selected . '<br/><b>Number of windows / Doors:</b> '. $doors . '<br/><b>How did you hear about our company?</b> '. $hear . '<br/><b>Additional Information:</b> '. $info;



    $mail->Send();

    echo "<div class='alert alert-success'  role='alert'>Thanks " . $firstname . ". Your message has been sent.</div>";

} # end if no error
else {

    $response = (isset($error['firstname'])) ? "<div class='alert alert-danger'  role='alert'>" . $error['firstname'] . "</div> \n" : null;
    $response .= (isset($error['email'])) ? "<div class='alert alert-danger'  role='alert'>" . $error['email'] . "</div> \n" : null;
    $response .= (isset($error['phone'])) ? "<div class='alert alert-danger'  role='alert'>" . $error['phone'] . "</div>" : null;

    echo $response;
} # end if there was an error sending

?>

The code 编码

// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
    echo $selected."</br>";
}

seems a bit odd to me. 对我来说似乎有点奇怪。 This displays all the values all right, but does not store them. 这将显示所有正确的值,但不存储它们。 After this loop, $selected will have as value the last element of the array. 在此循环之后, $selected将具有数组的最后一个元素作为值。 So, nothing is stored , what the comment suggests. 因此,什么也没有存储 ,注释表明了什么。

In the body of your mail, you're using $selected . 在邮件正文中,您正在使用$selected This has some arbitrary value from the array, or is uninitialised if the array was empty. 它具有数组中的任意值,如果数组为空,则未初始化。 What you could do instead is implode(', ', $_POST['check_list']) , which joins the elements of the array with commas in between (ie [4,7,8] becomes "4, 7, 8" ). 您可以做的是implode(', ', $_POST['check_list']) ,它以逗号之间连接数组的元素(即[4,7,8]变成"4, 7, 8" [4,7,8] "4, 7, 8" ) 。

If that still doesn't work, the value isn't properly passed into $_POST . 如果仍然不起作用,则该值未正确传递到$_POST You could do a var_dump($_POST) in the top of your file to see if the check_list values actually appear there. 您可以在文件顶部执行var_dump($_POST) ,以查看check_list值是否实际出现在其中。 You can include the output of that var dump in your question if you need help with it. 如果需要帮助,可以在问题中包含该var dump的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM