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 trial
cory Kelly
1,481 PointsContact form Newbie, Please Help!
Hey, I am having some trouble with a contact form that I built. I am having backend isuess. My Php is working but I've added fields that It does not recognize and I'm to new to know what's wrong.. Could you please have a look at my code? PLEASE! Thanks
html
<div class="span4 featured">
<h2 class="feature-title">Enter Info Below</h2>
<form action="send-mail.php" method="post" id="contact-form">
<p>Donβt forget a PO#!</p>
<input onfocus="this.value=''" type="text" value="Insert Company name" name="name" class="required">
<input onfocus="this.value=''" type="text" value="Insert Vessel name" name="name" class="required">
<input onfocus="this.value=''" type="text" value="Insert Crew name" name="name" class="required">
<input onfocus="this.value=''" type="text" value="Location" name="name" class="required">
<select>
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
<input onfocus="this.value=''" type="text" value="Insert your PO" name="phone">
<input onfocus="this.value=''" type="text" value="Insert your Comments" name="address">
<input type="submit" name="Submit" value="Submit">
<input class="reset" type="reset" value="Reset">
</form>
</div>
</html>```
Php
=======================================
```<?php
$mailto = '@txt.att.net'; // Enter your mail addres here.
$subject = 'Time to goto work!'; // Enter the subject here.
$error_message = 'Error sending your message'; // Message displayed if an error occurs
$success_message = 'Message Sent'; // Message displayed id the email has been sent successfully
$insertCompanyname = stripcslashes($_POST['Insert Company name']);
$insertVesselname = stripcslashes($_POST['Insert Vessel name']);
$insertVesselname = stripcslashes($_POST['Insert Vessel name']);
$insertCrewname = stripcslashes($_POST['Insert Crew name']);
$location = stripcslashes($_POST['Location']);
$insertyourPO = stripcslashes($_POST['Insert your PO']);
$dropdown = $POST['dropdown'];
//$message = stripcslashes($_POST['message']);
$message = "Name: $name\r\nEmail: $emailAddr\r\nPhone: $phone\r\nAddress: $address";
$headers = "From: $name <$emailAddr>" . "\r\n" . "Reply-To: $emailAddr" . "\r\n" . "X-Mailer: PHP/" . phpversion();
function validateEmail($email) {
if(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
return true;
else
return false;
}
if((strlen($name) < 1 ) || (strlen($emailAddr) < 1 ) || validateEmail($emailAddr) == FALSE ) {
echo($error_message);
} else {
if( mail($mailto, $subject, $message, $headers) ) {
echo($success_message);
} else {
echo($error_message);
}
}
?>```
1 Answer
don bencilao
4,153 Pointsdude I think you're naming your input text wrong. You should have different names for each input text field.
<input onfocus="this.value=''" type="text" value="Insert Company name" name="CompanyName" class="required">
<input onfocus="this.value=''" type="text" value="Insert Vessel name" name="VesselName" class="required">
<input onfocus="this.value=''" type="text" value="Insert Crew name" name="CrewName" class="required">
<input onfocus="this.value=''" type="text" value="Location" name="Location" class="required">
$insertCompanyname = $_POST['CompanyName'];
$insertVesselname = $_POST['VesselName'];
$insertCrewname = $_POST['CrewName'];
$location = $_POST['Location'];
I don't know why you are stripslashing your post value, I don't even know what that is for. I'm also a beginner, but I hope this helps.
I think you should also check out the phpmailer library. Randy Hoyt the teacher in php course is really good.