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 trialBen Mair
8,728 PointsPHP Code Question
So i'm having issues with my process_form.php page to work and convert my HTML form into an email for my website. here is my PHP code, what am I doing wrong?
<?php if (isset($_POST["submit"])) { $name = $_POST['First_Name + Last_Name']; $email = $_POST['Email']; $subject = $_POST['subject']; $message = $_POST['message']; $from = 'form-control'; $to = 'Ben@code3solutions.us'; $subject = 'Message from Code 3 Solutions Contact Form ';
$body = "From: $name\n E-Mail: $email\n Subject:\n $subject\n Message:\n $message";
// Check if name has been entered
if (!$_POST['First_Name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['Email'] || !filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email if (!$errName && !$errEmail && !$errMessage) { if (mail ($to, $subject, $body, $from)) { $result='<div class="alert alert-success">Thank You! I will be in touch</div>'; } else { $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>'; } } }
?>
10 Answers
Kevin Korte
28,149 PointsOkay, a check these out:
Your html isn't complete, and you never closed your form element. I think you're missing closing two divs and the form element.
Verify your action. I don't have enough info to be certain, but it's probably right. Just know that from root, there needs to be a
_vti_cnf
folder, and inside that, aprocess_form.php
file. With the forward slash at the beginning of the action, it means you're looking for that file from root, not relative to wherever the location of the file that has this form in it on the server.$_POST["submit"]
is never true, because your button doesn't have a name of submit, so modify your button html to be<button class="btn btn-default margin-bottom-9" type="submit" name="submit">Submit</button>
-
Subject will always be empty, because it doesn't have a
value
attribute on each option. That's what the form will submit, so that needs to be added. Look at my modified html.a. You gave the subject select a name of
Subject
with a capital S, but in your post you’re looking for the lower case version. These names are case sensitive, so$_POST[‘subject’]
would never be anything. I modified the html to for the select to bename=”subject”
so it works.b. However, you’d never get the value of the select sent through your form, since you’re setting the value of the variable
$subject
to$_POST[‘subject’]
, and than later one, you’re setting$subject
again this time to just a string. The Subject variable gets overridden, no Bueno. I think maybe what you wanted was something like `$subject = $subject . ‘: Message from Code 3 Solutions Contact Form’; which would than give you an email subject like “Customer Support: Message from Code 3 Solutions Contact Form” Message will always be empty, is there suppose to be a messages field in the form? I added a text area to your form just because it makes sense.
$from
is just a string, “form-control”, which probably won’t send out cause it’s not a valid email address. I would assume you want the from field to be the user’s email address, which we already grabbed in the$email
variable.For ease of readability, I modified your body string a little bit
In your error checking, `$_POST[‘message’] comes out of no where. I’m assuming it should be checking if $_POST[‘info_message’] is set.
Check if any error’s. You if statement was actually erroring, because if all 3 fields had data in them, then the variables
$errName
,$errEmail
, and$errMessage
never get set, howver you’re asking if they are true or false, which php doesn’t know, cause they don’t exists. So I modified to ask if they’re not set.
So if $errName
, and $errEmail
, and $errMessage
is not set, than proceed to sending the email. If any of them are are set, this check fails, and we do something in the else where I have the simple echo (“Sorry, check your fields”).
Now in Theory, this should send an email, but it’s still untested. You will still need a way to return the results back to the browser. Right now you are creating a result variable, but there is no way for it to get back to the client. Usually, and somewhere in Treehouse there are videos on forms in PHP, you set a get variable, and then redirect the header using that get variable to show or hid messages on form submit. You’re also not really checking the email address is correct at all, so just be aware this is little validation of fields here, other than is there something set, even if it’s just jibberish.
But you’re on your way, you can take it over from here. My modified code below:
<form method="post" action="_vti_cnf/process_form.php" class="box-aside" role="form">
<div class="row">
<div class="col-sm-6">
<div class="content-left">
<article class="content">
<div class="form-group">
<label for="firstname">First name:</label>
<input id="firstname" class="form-control" name="first_name" placeholder="First Name" type="text">
</div>
<div class="form-group">
<label for="lastname">Last name:</label>
<input id="lastname" class="form-control" name="last_name" placeholder="Last Name" type="text">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input id="email" class="form-control" name="email" placeholder="Enter Email" type="email">email
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea id="message" class="form-control" name="info_message" placeholder="Enter Message" type="text"></textarea>
</div>
<div class="form-group">
<label>Subject</label>
<select class="form-control" name="subject">
<option value="Customer Support">Customer Support</option>
<option value="Sales Marketing">Sales Marketing</option>
<option value="Job Opportunities">Job Opportunities</option>
<option value="Product Support">Product Support</option>
<option value="Suggestions">Suggestions</option>
</select>
</div>
<button class="btn btn-default margin-bottom-9" type="submit" name="submit">Submit</button>
</article>
</div>
</div>
</div>
</form>
<?php
if (isset($_POST["submit"])) {
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['info_message'];
$from = $email;
$to = 'Ben@code3solutions.us';
$subject = $subject . ': Message from Code 3 Solutions Contact Form ';
$body = "From: $firstname\n";
$body .="E-Mail: $email\n";
$body .="Subject: $subject\n";
$body .="Message: $message\n";
// Check if name has been entered
if (!$_POST['first_name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['info_message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!isset($errName) && !isset($errEmail) && !isset($errMessage)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
echo ($result);
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
echo($result);
}
} else {
echo ("Sorry, check your fields");
}
}
?>
Ben Mair
8,728 PointsThey are all $_POST, I am using a bootstrap theme in dreamweaver and opened the process_form.php file to add the code to output the data. I put the following code in it and i get a 404 error with no emails being sent. can you try to point me in the right direction?? I'm at a loss, i've been trouble shooting for hours.
Ashish Mehra
Courses Plus Student 33 Pointscheck your file is it in the same directory were you want. May be there will be some issue with location of file because of which you are getting error as 404 ...
Ben Mair
8,728 Pointsit's in my _vti_cfn folder........Ive never had to code this portion of it as I have been using wordpress.......So i'm not exactly sure where that folder should sit, it is just sitting in there as that is whrere it was when I found it.
Kevin Korte
28,149 PointsThis is invalid $POST['First_Name + Last_Name']
.
On you name, field, is first_name and last_name one field, or two separate fields you're trying to combine? Also make sure it's $_POST
.
What does your HTML form look like?
Ben Mair
8,728 Points<form method="post" action="/_vti_cnf/process_form.php" class="box-aside" role="form">
<div class="row">
<div class="col-sm-6">
<!-- CONTENT START -->
<div class="content-left">
<article class="content">
<div class="form-group">
<label for="firstname">First name:</label>
<input id="firstname" class="form-control" name="first_name" placeholder="First Name" type="text">
</div>
<div class="form-group">
<label for="lastname">Last name:</label>
<input id="lastname" class="form-control" name="last_name" placeholder="Last Name" type="text">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input id="email" class="form-control" name="email" placeholder="Enter Email" type="email">email
</div>
<div class="form-group">
<label>Subject</label>
<select class="form-control" name="Subject">
<option>Customer Support</option>
<option>Sales Marketing</option>
<option>Job Opportunities</option>
<option>Product Support</option>
<option>Suggestions</option>
</select> </div>
<button class="btn btn-default margin-bottom-9" type="submit">Submit</button>
</article>
</div>
Ben Mair
8,728 Points<?php
if (isset($_POST["submit"])) {
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['info__message'];
$from = 'form-control';
$to = 'Ben@code3solutions.us';
$subject = 'Message from Code 3 Solutions Contact Form ';
$body = "From: $firstname\n E-Mail: $email\n Subject:\n $subject\n Message:\n $message";
// Check if name has been entered
if (!$_POST['first_name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Ben Mair
8,728 PointsI keep gettting a site definition error in dreamweaver and after talking to godaddy they stated that the php has not hit their server(duh) and recommend a syntax error. I'm thinking i'm missing a line a code somewhere and possibly a step to set the form up?
Ben Mair
8,728 PointsTHis has helped, however not an exact answer.......I am working through the problem now, I did have close form, there was a portion missing as it's a split form with message placed to the right. so I didn't copy that code. Finally not showing a 404 error code, but still not sending an email.
Kevin Korte
28,149 PointsWell what does the screen show? I put those 3 echo statements in there so you know which one happened. Or is there an error message still, and if so what does that say?
Ben Mair
8,728 PointsThe screen shows nothing.....in dreamweaver I can see the echo coding on the screen, however when I submit a form it's just a blank form, it goes to the process_form.php file but it's obviously not running.
Ben Mair
8,728 PointsI figured it out Kevin, the $from needed to be process_form.php and n$email;
Ashish Mehra
Courses Plus Student 33 PointsAshish Mehra
Courses Plus Student 33 PointsHey Ben Mair, In first above lines you are using $POST instead of $_POST & if you want to send html email along with css you need to have to include content type and MIME version in your header file...
Thanks I think it will help you , If you still found some issue let me know