Reputation: 43
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
E-mail: [email protected]
Phone: 123-652-9658
Street Address: 123 Main Street
City: Albany
State: NY
Zip: 12345
Areas of Interest:
Number of windows / Doors: 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
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 protected]'; // 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
?>
Upvotes: 0
Views: 95
Reputation:
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.
Upvotes: 1