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

时间:2015-04-24 21:24:13

标签: php html forms checkbox

在这种形式中我正在构建我在提交表单时在网站所有者收到的电子邮件中列出数组时遇到问题。以下是收到的电子邮件的外观:

公司:公司名称
名字:鲍勃 姓氏:琼斯 电子邮件:email@gmail.com
电话:123-652-9658
街道地址:123 Main Street
城市:奥尔巴尼
州:纽约州 邮编:12345
兴趣范围:
窗户数/门数:6
你是怎么听说我们公司的? google bing
附加信息:嗨

以下是表单代码:

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

以下是运行表单的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

?>

1 个答案:

答案 0 :(得分:1)

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. So, nothing is stored, what the comment suggests.

In the body of your mail, you're using $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 (i.e. [4,7,8] becomes "4, 7, 8").

If that still doesn't work, the value isn't properly passed into $_POST. You could do a var_dump($_POST) in the top of your file to see if the check_list values actually appear there. You can include the output of that var dump in your question if you need help with it.