Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialkayesspea
8,907 PointsContact form data isn't showing in PHP email.
I've created a simple form to go on my website, and a simple PHP file to send the form to my email. Most variables show up when the email goes through, but two don't.
Here is the form:
<div class="form-group col-lg-4"> <label for="Name">Your Full Legal Name</label> <input type="text" class="form-control" id="Name" name="Name" required> </div> <div class="form-group col-lg-4"> <label for="FianceName">Your Fiance's Full Legal Name</label> <input type="text" class="form-control" id="FianceName" name="FianceName" required> </div> <div class="form-group col-lg-4"> <label for="Email">Your Email Address</label> <input type="email" class="form-control" id="Email" name="Email" required> </div> <div class="form-group col-lg-4"> <label for="Phone">Your Phone Number</label> <input type="tel" class="form-control" id="Phone" name="Phone" required> </div> <div class="form-group col-lg-4"> <label for="When">Ceremony Date</label> <input type="date" class="form-control" id="When" name="When" required> </div>
<div class="form-group col-lg-4">
<label for="City">City Where Ceremony Will Occur</label>
<input type="text" class="form-control" id="City" name="City" required>
</div>
<div class="form-group col-lg-4">
<label for="Venue">Ceremony Venue</label>
<select class="form-control" id="Venue" id="Venue" required>
<option class="option" value="" selected></option>
<option class="option" value="InHome">In Home</option>
<option class="option" value="InChurch">In Church</option>
<option class="option" value="EventVenue">Event Venue</option>
<option class="option" value="Unsure">Unsure</option>
<option class="option" value="Other">Other</option>
</select>
</div>
<div class="form-group col-lg-4">
<label for="Rehearsal">Will the officiant be required for the rehearsal?</label>
<select class="form-control" id="Rehearsal" name="Rehearsal" required>
<option class="option" value="" selected></option>
<option class="option" value="Yes">Yes</option>
<option class="option" value="No">No</option>
<option class="option" value="Unsure">Unsure</option>
</select>
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label>Additional Questions or Details</label>
<textarea class="form-control" rows="6" id="Message" name="Message" ></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button type="submit" class="btn btn-default" id="submit" name="submit" type="submit">Submit</button>
Here is the PHP:
<?php
if (isset($_POST['submit'])) { $to = 'keisha@kayesspeadesign.com'; // Use your own email address $subject = 'Someone emailed you about officiatins services';
$Name = $_POST['Name'];
$FianceName = $_POST['FianceName'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone'];
$When = $_POST['When'];
$City = $_POST['City'];
$Venue = $_POST['Venue'];
$Rehearsal = $_POST['Rehearsal'];
$Message = $_POST['Message'];
$From = 'From: I Do Marry You';
$Body = $Name . " and " . $FianceName . " have sent you the following information regarding their wedding:" . "\r\n\r\n";
$Body .= "Location: " . "\r\n" . $City . "\r\n\r\n";
$Body .= "Phone Number: " . "\r\n" . $Phone . "\r\n\r\n";
$Body .= "Email: " . "\r\n" . $Email . "\r\n\r\n";
$Body .= "When: " . "\r\n" . $When . "\r\n\r\n";
$Body .= "Location: " . "\r\n" . $City . "\r\n\r\n";
$Body .= "Venue: " . "\r\n" . $Venue . "\r\n\r\n";
$Body .= "Will the officiant be required to attend the rehearsal? " . "\r\n" . $Rehearsal . "\r\n\r\n";
$Body .= "Additional Questions and Details: " . "\r\n" . $Message;
$success = mail($to, $subject, $Body);
} ?>
The result is that the "Venue" and "Message" variables don't show up in the email. I'm very new to PHP and I'm sure there's some small detail I've missed. Your help is greatly appreciated.
kayesspea
8,907 PointsWow! Thanks! Good to get another set of eyes on it, because I didn't see that.
Thanks again, Manav!
1 Answer
Benjamin Payne
8,142 PointsHey Keisha, Try this code out. I verified that it works using your form. I went ahead and filtered the inputs from the form as well. You should always use some sort of filtering on inputs. Also, I changed the logic around a bit for checking the post method. This way you can programmatically return an error to the customer. Let me know if this fixes the textarea issue.
Thanks! Ben
<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { return; }
$to = 'keisha@kayesspeadesign.com'; // Use your own email address
$subject = 'Someone emailed you about officiating services';
$Name = filter_input(INPUT_POST, 'Name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$FianceName = filter_input(INPUT_POST, 'FianceName', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$Email = filter_input(INPUT_POST, 'Email', FILTER_VALIDATE_EMAIL);
$Phone = filter_input(INPUT_POST, 'Phone', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$When = filter_input(INPUT_POST, 'When', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$City = filter_input(INPUT_POST, 'City', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$Venue = filter_input(INPUT_POST, 'Venue', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$Rehearsal = filter_input(INPUT_POST, 'Rehearsal', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$Message = filter_input(INPUT_POST, 'Message', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$From = 'From: I Do Marry You';
$Body = "$Name and $FianceName have sent you the following information regarding their wedding:\r\n\r\n";
$Body .= "Location:\r\n $City \r\n\r\n";
$Body .= "Phone Number: \r\n $Phone\r\n\r\n";
$Body .= "Email: \r\n $Email \r\n\r\n";
$Body .= "When: \r\n $When \r\n\r\n";
$Body .= "Location: \r\n $City \r\n\r\n";
$Body .= "Venue: \r\n $Venue \r\n\r\n";
$Body .= "Will the officiant be required to attend the rehearsal?\r\n$Rehearsal\r\n\r\n";
$Body .= "Additional Questions and Details:\r\n$Message";
$success = mail($to, $subject, $Body);
Manav Misra
11,778 PointsManav Misra
11,778 PointsNot sure about the textarea issue, but for 'Venue', you don't have 'name' assigned correctly; From your code above: <select class="form-control" id="Venue" id="Venue" required>
You need to change one of those 'id=' to 'name='