Conversions don’t always equate to purchases or transactions, they can also appear in the form of newsletter sign-ups and contact forms. Contact forms are one of the most common features on a website. Standard contact forms work just fine, but you can make them nicer by using AJAX to submit the form data in the background.
In this blog post, you’ll learn how to create a website contact form that submits the form data using AJAX. We’ll be using jQuery to help simplify the JavaScript code and a simple PHP mailer script to handle sending the form data via email.
Let’s get started.
Contents
Step 1: Building the HTML Form Using jQuery
We’ll start by setting up the HTML form that will collect data from the user. Give your <form>
element the ID ajax-contact
, set the method
attribute to post
, and the action
attribute to mailer.php
.
<form id="ajax-contact" method="post" action="mailer.php">
<div class="field">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="field">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="field">
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
</div>
<div class="field">
<button type="submit">Send</button>
</div>
</form>
Here you’ve set up a simple form that has fields to collect the user’s name, email address, and message. Notice that each of the form fields has a required
attribute. In browsers that support HTML5 form validation, this will prevent the form from being submitted until all the fields have been completed.
Next you need to create a <div>
element that will be used to display success and error messages to the user. Create this element above the <form>
in your HTML markup and give it the ID form-messages
.
<div id="form-messages"></div>
Now you need to download the code resources and copy the style.css
file into your project directory. You’ll also need to add a <link>
element that tells the browser to load the stylesheet.
<link rel="stylesheet" href="style.css">
Finally you need to create two <script>
elements that reference the jquery-2.1.0.min.js
and app.js
files. Add these just before the closing </body>
tag.
<script src="jquery-2.1.0.min.js"></script>
<script src="app.js"></script>
It’s important that you load the jquery-2.1.0.min.js
file first as the code within app.js
requires jQuery.
That’s all the HTML you’re going to need in this post. Next up, let’s take a look at the JavaScript.
Step 2: Submitting the Form Using AJAX
Create a new file in your project directory called app.js
. This is going to contain all of the code responsible for submitting the form data using AJAX.
Copy the following code into the app.js
file.
$(function() {
// Get the form.
var form = $('#ajax-contact');
// Get the messages div.
var formMessages = $('#form-messages');
// TODO: The rest of the code will go here...
});
Here you’ve created two new variables, form
and formMessages
, that reference the corresponding elements in your HTML markup.
Next you need to create an event listener that will intercept submit
events on the form. You can do this using the jQuery submit
method.
// Set up an event listener for the contact form.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
// TODO
});
Here you’ve passed a function to the submit
method that will be executed when the user submits the contact form. You’ve also told the browser not to submit the form as it would normally by calling the preventDefault
method on the event.
Next you need to serialize the form data. This will convert the data the user has entered into the form into a key/value string that can be sent with the AJAX request. Use the jQuery serialize
method to serialize the form data and then store the result in a variable called formData
.
// Serialize the form data.
var formData = $(form).serialize();
Now you’re ready to write the code that’s responsible for sending the form data to the server and processing the response. Copy the following code into your app.js
file.
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
Here you’re using jQuery’s ajax
method to create a new AJAX request. You’ve passed an object to the ajax
method that contains a number of properties used to configure the request. The type
property specifies the HTTP method that will be used for the request, in our case POST
. The url
property is the location of the script that the form data will be sent to. You’ve populated this property by fetching the action
attribute from the form. Finally, the data
property has been populated using the formData
variable that you created earlier.
Next you need to handle a successful response from the server. Copy the following code directly after the closing bracket of the ajax
call. Note that I’ve deliberatly left out the semi-colon.
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#message').val('');
})
This done
method will be called if the request completes successfully. Here you first make sure that the formMessages
element has the success
class and then set the text content of this element using the data returned by the mailer script. To finish, you clear out the values from each of the form fields.
The last bit of JavaScript you need to write handles what should happen if an error occurs. Copy the following into app.js
.
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
This fail
method is called if the mailer script returns an error. Here you first make sure that the formMessages
element has the error
class. You then check to see if the AJAX request returned any responseText
. If it did, you use this text to set the content for the formMessages
element; otherwise use a generic error message.
That completes the HTML and JavaScript code that’s needed to build an AJAX contact form. In the next section you’re going to learn about the mailer script that is responsible for processing the form data and sending out an email.
Step 3: Creating the PHP Mailer Script to Send Emails
Now it’s time to write the PHP mailer script that will process the form data. This simple script is responsible for checking that the form data is valid and then sending out the email. If an error occurs, the mailer script will respond with an appropriate status code so that the JavaScript within the fail
callback you wrote earlier will be executed.
Create a new file called mailer.php
and copy into it the following code.
<?php
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "hello@example.com";
// Set the email subject.
$subject = "New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
?>
Update: Thanks to Aaron Traas for his modifications that make this mailer script more secure.
This script starts by checking that the request was sent using the POST
method. If it wasn’t, the script will return a 403
(forbidden) HTTP status code and an error message.
Once you’ve established that the correct HTTP method has been used you then extract the form data into three variables $name
, $email
, and $message
. You also use the PHP trim
method here to cut out any unwanted whitespace.
Next you will check to make sure that none of these variables is blank. If one or more is blank, set the response code to 400 (bad request) and return an error message to the browser.
Now that you’ve determined the data is clean, you prepare the email, first by creating a variable with the email recipient. You then create variables for the subject, email content, and finally the email headers.
Note: Setting the email headers is optional. Doing so will make the email appear as though it was sent by the person that filled out the form. However, it’s important to note that manipulating the headers like this can also cause the email to be marked as spam by some email clients.
Next you will attempt to send the email using the PHP mail
function. If this is successful, you will return a success message. If it’s not, you need to set the response code to 500
(internal server error) and return an error message.
Note: The standard PHP mail
function will do just fine for the purposes of this blog post, but there are more robust ways of sending email using PHP. If you’re interested in learning more, I recommend that you check out the Build a Simple PHP Application project on Treehouse.
That’s it! You’re all done.
Load up your HTML file in a web browser and try out the form. You’ll need to be using a web server with support for PHP and the mail
function for everything to work correctly.
Final Thoughts
In this blog post, you’ve learned how to create a contact form that uses AJAX to communicate with a mailer script on the server. You’ve used jQuery to help simplify the JavaScript code, but you could have achieved the same result with standard JavaScript.
Using AJAX within your websites allows you to improve the user experience in a number of different areas, not just contact forms. Next time you’re building a website, ask yourself how AJAX could help to create a smoother experience for your users.
Useful Links
If you’re looking to take your JavaScript or HTML5/CSS programming skills to another level, check out our Techdegrees in Full Stack JavaScript and Front End Web Design. Our faculty of tech professionals guide learners like you from mastering the fundamentals of coding to polishing the skills of a job-ready software developer. Try one of them out with a free seven-day trial today.
thanks so much!!!
Thanks so much! This was very helpful!
Thank you 🙂
THANK YOU SO MUCH my project deadline is coming and you saved my life
Great tutorial. A Github link or download would be nice to avoid having to piece the code together.
Thanks tho!
Thank you, it is very useful! I highly appreciate your help!
Thank you so much and it is really works. The explanation is really easy to understand for a newbie like me hehee. Thanks 🙂
I am a junior developer and I learned a lot with this great and easy to understand tutorial.
Thank you very much. It works perfect 🙂
Hey there, thank you for the code. I have found that when using it some submissions are going through and some are not. They’re not being caught in my email’s spam filter, they’re just not showing up at all. What could cause this?
hi there
i just wish to get rid of the php processing – -and implement it in javascript as well or combination of js html/ejs files from node.js express (jquery if needed)– any help ideas – appreciate thx
Hola agradezco tu aporte para que se pueda aprender, mira estoy tratando hacer jalar tu ejemplo todo parece estar al pie de la letra, además de compararlo con el recurso que proporcionas, pero me aparece el siguiente error:
http://grupodehum.mx/conferencistas-humanistas/ajax-contact-form-demo/mailer.php 500 (Internal Server Error)
en el archivo mail. php puse únicamente el correo al que quiero que lleguen los correos y probe con hotmail, gmail y aparece el mensaje “Oops! Something went wrong and we couldn’t send your message.”, que estoy haciendo mal? agradeceré tu apoyo, saludos
Intenta utilizar la libreria PHPMailer, con ella podrias solventar tus problemas de envío.
Hi, I have this enabled on a couple of other website but for some reason Im getting the “Ooops! An error occured and your message could not be sent” message on this one. The form is actually posting and the email is being delivered but Im getting the wrong message displayed after hitting send. It seems like the .done and .fail functions are not working. Has anybody else experienced this?
Hi,
I am using xampp server, After Submitting the form I am getting the message “Thank You! Your message has been sent.” But I didn’t get any email, I had changed the recipient content in mailer.php with my email address. can you please help.
Thanks in Advance.
hello, your mail cant deliver using xamp, instead host it online and the mail will be sending ,
your xamp dosnt have any mail server to send the message
thats why
In the Javascript part there is no need to call $() function each time you want to access the DOM elements.
The variables “form” and “formMessages” you created already contain references to them.
More simply :
…
form.submit(…
…
formMessages.removeClass(‘error’);
…
Thanks a lot Matt, years has been past and it’s still works great !
I made few changes and it’s functioning well in my website, see demo here:
https://www.mike652638.com/demo/mail-form.html (In Chinese Language however :))
Hi, I hope somebody can help me. I’m not a programmer, I only did exactly what was described here.
The email message actually gets sent once everything is filled out and submitted correctly. But here are the problems I couldn’t solve yet.
1. The “required” attribute in the HTML form seems to work fine in Chrome but it doesn’t work in Safari. Safari will let me submit the form and then show the error message on of the mailer.php on a blank page “Oops! There was a problem with your submission. Please complete the form and try again.”
2. No matter if a submission is successful or not the result message will always be shown on a blank page (with …/mailer.php) in the address bar). Of I course I want to have the success or non-success message shown on my styled page where the HTML form is located and not on a blank page.
I hope somebody can help me. Thanks in advance.
Having the same issue with displaying the result on the php page instead of the first html page. Did you figure out anything?
Please I need your help, I got and ERROR like this (I’m using WAMP server) Thanks
( ! ) Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Users\cysca\projects\SiteWeb\mailer.php on line 38 Call Stack #TimeMemoryFunctionLocation 10.0010251560{main}( )…\mailer.php:0 20.0012252920mail ( )…\mailer.php:38 Something went wrong and we couldn’t send your message.
I think you will find it doesn’t send because most “local hosts” like Xampp, wamp etc. do not have a mail server built into them.
You may need to find (if you don’t have one already) an online server and load your files to there then give it a run – this is just a suggestion, and a possible solution
Hi,
Why do we need to mention method=”post” action=”mailer.php” within form itself if we are using jQuery ajax for the same?
Thanks
Hi. Your codes are very useful but I have a problem, I received the email if I put my email in the $recipient = “sample@gmail.com”; but if I changed the recipient nothing happened. Can you please help me ?
Thank you !
Hello there.
Just a quick fix:
I changed this:
$(function() {
to:
jQuery(document).ready( function ($) {
and everything worked like wonder 🙂
Hope it helps
This works good, thank you!
I get a `405 (method not allowed)` error. Could this have something to do with me running the site on a development server I set up in terminal?
Here is a link to my GitHub repo: https://github.com/sebastianlofaro/Colleen-Neal . The project has a long way to go…
Any help would be much appreciated!
Great post. I’m experiencing many of these issues as well..
Nevermind, I found the conflict, it was with “modal”.
I had this code on my site working fine until I integrated the a new design. The bootstrap.min.css file is causing a conflict. The form only works when I remove it. Not sure how to find the exact spot of the problem.
Thanks!
Javi
Hi,
I did everything explained in your tutorial, but it’s not working.
First of all, it go to my mailer.php instead of staying on my HTML page.
Secondly, I always get the first error about (empty($name)…). My fields are not empty…
Thank you
Also, I put the empty verification in comments, but email is sending two times instead of one. Anybody?
I copied the code into app.js but the order of the jQuery was unclear because of all the // TODO’s. When I submit the data it sends me to the mailer.php page, rather than remaining on the contact form page. So next I copied the .js from James’ comment and tried it – same result. Can you post a copy or link to the app.js script? Tx
Hi Dmitri,
Of course. You can find a full copy of all the code here: https://github.com/matt-west/ajax-contact-form
I have created a contact form for my website but had a few nagging issues I wanted to clear up, big thanks for posting.
Ajax is very hard. But I’m trying to make it. Thanks for information.
Thanks for the great form script Matt. It works well for me and everytime i’ve tested.
But typically someone is managing to get to the error message “Oops! An error occured and your message could not be sent.” which is in app.js.
I can get some of the other error messages which are picked up earlier but really cant replicate this. How could someone trigger this if they’ve filled the form in right? Could it be a shared hosting issue?
In theory could the piece of code that governs this in app.js be taken out entirely and perhaps fix this?
.fail(function(data) {
// Make sure that the formMessages div has the ‘error’ class.
$(formMessages).removeClass(‘success’);
$(formMessages).addClass(‘error’);
// Set the message text.
if (data.responseText !== ”) {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text(‘Oops! An error occured and your message could not be sent.’);
}
});
Thanks!
Hi Paul,
Try changing this line:
$email_headers = “From: $name <$email>“;
to:
$email_headers = “Reply-To: $name <$email>“;
There was recently some changes to a few different mail providers that no longer allow the From header to be spoofed. Using Reply-To instead should work.
I have got the same thing.
I have done the changes, but it doesn’t help.
Hello!
After I was creating js file with your tutorial, then I’ve got content of the php file, I mean simple test message I wrote there.
When I filled the php file with the right code, then I get only adding “success” CSS class on message block, but nothing I receive on mail?
Why was that happen?
Thank you
Hi Paul! As this is an older post, I’d recommend posting your question in the Treehouse Community. Treehouse students and teachers are always around and happy to help!
Thanks for the tutorial Matt! Just one question, why are you using the strip_tags function in your php?
The tutorial was very nice & easy to follow. Some type of captcha would be great with more protection on server side e.g. XSS if working with database,etc. So, it would be secure for all types of forms e.g. signup,login,etc.
Thanks man, finally an AJAX form script that actually works. You made my day!
Thanks for the snippet! However, emails from Yahoo addresses do not send. The message shows that it sent, but it does not actually send. Is there a fix for this? (This has happened multiple times)
Hi Kyle! As this is an older post, I’d recommend posting your question in the Treehouse Community where Treehouse students and teachers are always happy to help. 🙂
I have the same problem, is their a solution known? Thanks
Hey! First, thank you for providing this form! By no means am I a developer who would’ve been able to build this on my own and getting a contact form to work properly has turned out to be one of the most challenging things to find info for online.
Everything with the form works as is. But, I’m trying to modify the code to get have the page redirect to a thank you page after successful submission. Any idea how I might be able to make this happen?
So far, I’ve tried modifying the mailer.php file and app.js. Any help would be greatly appreciated!
Hi Aaron! As this is an older post, I’d recommend asking your question in the Treehouse Community. Treehouse students and teachers are always happy to help. 🙂
Aaron,
AJAX is specifically for loading/refreshing page elements individually, without having to reload the whole page. The same-page completion is intended here.
If you want to be redirected to a separate ‘thank you’ page, you’ll have to look for a php mail script without AJAX.
Hello, Everything works perfectyl, but I have got one main problem – polish signs. Do you know where to add charset=utf-8′) in order to have polish letters? PLEASE help 🙂
Hi there! I’d suggest asking your question in the Treehouse Community. Treehouse teachers and students are always eager to help answer questions. 🙂
HI Aaron, I’m no developer either, but I think Ajax automatically creates the “Thank you message” on the same page. I was trying to work on the contact form so it won’t redirect to a new page and some people on the forum told me to use Ajax. I think if you want to create a “Thank you” page that redirects to a new page, maybe it’s best not to use Ajax for that. But that’s just my guess! Go to this forum and check it out and post your questions on here. People respond pretty quick.
https://www.sitepoint.com/community/t/my-php-works-but-error-messages-are-not/223516
I used php contact form and it always redirected to a new page. I hope this helped!
Sandy,
AJAX is specifically for loading/refreshing page elements individually, without having to reload the whole page. The same-page completion is intended here 😉
Can I follow this guide to create a contact form for WordPress? 🙂
Hi! Sorry my English. How use this form in cyrillic. When I change $subject = “New contact form от: $name”; (in mailer.php) to $subject = “Новое письмо от: $name”; that message not send to my mail?… but wihout changes all woorks good
Hi Nazar! As this is an older article, we’d recommend hopping over to the Treehouse Community to ask your question. Our teachers and students are always happy to help. 🙂
I’m not really a web designer or coder so thank you so much for this. It really helped me wrangle a template into operation!
That’s great to hear. Thanks for reading, John! 🙂
I’m no coder, but I notice that in the code block for the condition test in line 38, you don’t actually call the mail() function to send the mail.
Thanks.
Beautiful tutorial very good good demo there is another good tutorial for submitting form without page refresh using jquery http://talkerscode.com/webtricks/submit%20the%20form%20without%20page%20refresh%20using%20ajax%20and%20jquery.php
Hi,
I got the program run in a local machine, but it does not work correctly on a shared hosting environment. When the form is not filled in completely the program exits with the approriate message “Oops! There was a problem with your submission. Please complete the form and try again.”. However the header status is not set and the “done”-part of the jquery function is run.
PHP version is 5.6 so http_response_code should work. If I replace it with header(“HTTP/1.0 400 Bad Request”), this does not work either.
Looking at the POST headers in firebug I found, that on my local machine, the connection is “closed” whereas on the shared host it is “keep-alive”. So i tried to change the header in the $.ajax-call adding the line “headers: {Connection: close}” like this:
$.ajax({
type: ‘POST’,
url: $(form).attr(‘action’),
data: formData,
headers: {Connection: close}
…
and
$.ajax({
type: ‘POST’,
url: $(form).attr(‘action’),
data: formData,
beforeSend: function( xhr ) {
xhr.setRequestHeader( “Connection”, “close” );
}
…
but neither helped to change the connection.
Then i figured out, that the connection could be kept on the shared host due to longer connection time on the server. I tried to get the connection cut by changing the network.http.connection-timeout in firefox to a lower value. but this didnt help either.
Has anyone any ideas, what else could cause the problem?
Hi there! This would be a good question to ask in the Treehouse Community, where our teachers and students are always happy to help and share their feedback.
Thank you for the tutorial Matt, The form works great. I’ve added bootstrap validation and everything works well. The only problem that I’m having is once the form fields are filled correctly and the form is submitted. The email gets sent twice into my inbox. Is there any way to place a function in the php code that checks to see the email has been posted stop the operation.
Incorperated this form into my personal site but it just follows through to the PHP actions (ie going to a new page and echoing ‘your message has been sent’) rather than not reloading the page and providing the notification through AJAX.
Is this meant to do that? I added a console log to try and see if I could trigger it on send but it just jumps right to the PHP echo. An example of what happens can be found here: http://marcmurray.net/portfolio_test/#popUp
Anyone got any ideas?
I have the same issue with trying to post using AJAX and I would love a fix.
Is it possible to pass a form ID to this script so I can use it to have multiple forms on the same page?
First off thank you very much for this tutorial Matt, it is amazing! I was wondering if anyone could help me out as I am new to this. I followed this tutorial word for word. When I uploaded it to my server everything works perfect. The problem comes when I try to apply this code to a contact form I have created with the Foundation6 framework. I keep getting errors thrown left and right. If anyone could please test this and help me figure out what I am doing wrong that would be greatly appreciated. I am new to this so any help and direction would be amazing. The link to my contact form is:
http://www.seanmangosing.com/clients/FoundationPlayground/
Thank you again for all help that may come of this.
As a side note, the contact form will load with no errors, It is when I fill it out and hit the submit button, I get a 400 (Bad Request) error with conflicts to the jquery file.
Hi,
The form is not submitting. Keep getting the 403 forbidden response. I even loaded the exact copies of your files on the production server and still get the same error. My server has PHP 5.3. I’ve replaced the header http_response_code(403); to header(“HTTP/1.0 404 Not Found”); and the form is not submitting and giving the 403/404 message.
Try this solution:
http://stackoverflow.com/questions/3258634/php-how-to-send-http-response-code
Hi! I implemented this form on my website and everything works great, I get the emails, awesome, but my message appears on another page instead of in the same page which is how I wanted it. Any suggestions on how to fix this issue?
Hi,
I’ve implemented all javascript and php and the browser is bringing up the #form-messages as green and showing the success message, but I am not receiving the email. I have changed the recipient email in mailer.php, so I’m not sure what the issue is here…
Any help would be appreciated!
*Note: I’m running the page through MAMP Version 2.1.3 (2.1.3)
Update: I uploaded it to a remote server and it seems to have resolved the issue. The localhost seems to have had an problem with the code for some reason.
Anyway, problem solved 🙂 Thanks for a great tutorial!
First thank you for this good tutorial.
Now i am want to ask about the security about this form. Aren t there robots looking for buttons tags to press and send spam? Is there any way to solve this?
Hi,
Thanks for the great tutorial.
I used your code on my server and its working great. However when i inserted in a wordpress page its giving problem. Form is submitting but i am getting error message. If problem is with php version it should not work in other folder also. But its working perfectly in other folder. What could be the reason?
Is it possible due to javascript conflict with older version of jquery on the same page?
Just stopped by to give you some kudos, this tutorial is great and is exactly what I was looking for after 3 days of php torture.
Very appreciated!
Hi guys,
I am fairly new to JavaScript and jQuery and I want to find out if I have indented and put all my code in the correct places: My JavaScript is below:
$(function() {
// Get the form.
var form = $(‘#ajax-contact’);
// Get the messages div.
var formMessages = $(‘#form-messages’);
// TODO: The rest of the code will go here…
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
// TODO
});
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: ‘POST’,
url: $(form).attr(‘action’),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the ‘success’ class.
$(formMessages).removeClass(‘error’);
$(formMessages).addClass(‘success’);
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$(‘#name’).val(”);
$(‘#email’).val(”);
$(‘#message’).val(”);
})
.fail(function(data) {
// Make sure that the formMessages div has the ‘error’ class.
$(formMessages).removeClass(‘success’);
$(formMessages).addClass(‘error’);
// Set the message text.
if (data.responseText !== ”) {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text(‘Oops! An error occured and your message could not be sent.’);
}
});
});
Running into an issue where the form validates and the success message pops up, but the mail only actually sends some of the time. Tried recreating it with different email addresses and cannot recognize a pattern.
Currently running PHP 5.6. Does this sound like a host issue?
Hi. thanks.
Thanks for the tutorial! I have a question about URL validation.
If my HTML page had a text input called URL, where someone could paste a URL, what would the correct syntax be in mailer.php (around Line 14, I presume) to validate the URL? Or should it just be treated like a string, and submitted as long as it’s not blank? I can’t seem to find information on this online.
Thanks again.
Hi . Thanks for conveying your eloquent and simple
Hi,
Thank you for this great tutorial. I used this contact form tutorial for two contact forms in my site.
One form was a mailing list and one a standard contact form. When the site went live the mailing list worked but the contact form was coming up with a 500 (internal server error) response code. Now they are both not working. They worked fine when I tested them on the local server.
My host says it is because they don’t support the php mail function anymore. Is there anyway around this? I’m a bit stuck.
Thanks Clare
Any idea how to stop messages being sent as spam?
Hi Matt, really nice tutorial, thank you for this. Well laid out and easy to follow.
I am having a problem though. I have set it up and tested it, and the form is submitted and received by my email, but I am getting this message on my page once it has been sent.
Fatal error: Call to undefined function http_response_code() in /home/xxxxxx/public_html/privatelisting/mailer.php on line 40
What does this mean and how can I fix it? Many thanks!
Matt, just seen your other posts about this same issue. I have the most recent PHP version (5.6) and it’s still not working. Replacing it with header(“HTTP/1.0 404 Not Found”); doesn’t work, it just gives another error and the message doesn’t send at all now.
Could you possibly suggest another solution or one that I may understand? 🙂
Hi Chris,
What error do you get when you’re using the `header` method instead?
Matt, I have the same issue. I changed the header to header(“HTTP/1.0 404 Not Found”); and it give the same error message “There was a problem with your submission, please try again.” in your // Not a POST request, set a 403 (forbidden) response code comment.
Hey Matt,
thanks for this nice tutorial. It helped me by setting up my own tiny website! 🙂
Best
Chris
Hi Chris,
How did you resolve the Fatal error: Call to undefined function http_response_code()?
I have the same issue. Thanks in advance.
Great tutorial!
I am trying to add html tags to the response messages. But the tags appear as text. I can’t get it to work. Do you know if it is possible to use html tags in the error/success messages?
Use the .html() method instead of .text()
This is a great contact form and have been using it on my site, I was wanting to add a couple of fields to this contact form and radio subject, and an file_uploader. is this possible?
Matt West lives in a tree house. Go-o-o-o-o-o-o, Team!
Thanks for the tutorial. I was just wondering what and how the e-mail address is validated as it doesn’t accept just a few characters for example. Just wondering what it is that does that? thanks.
Can help me in buliding my website http://blogswindow.com/ like yours…Please share tips with me
I love this tutorial, and it seems clear in its instruction, however I myself am still having issues implementing the code. When hitting the submit button, and all required fields are filled out, the entire php file gets returned in the containing “form-messages”. If not filled out I get the js file’s fail error of “Oops! An error occurred…” just curious if there is something simple I may have done wrong. I even used your source code as a double check and get the same result.
This territorial is one of the better ones out there addressing the ideas around from submission without page refresh. As well nothing about it is hard coded; therefore is works perfectly as a function.
However, I am having an issue once the app.js file gets moved into a subdirectory or library. The code seems to break that that point and the form doesn’t submit. What is the code necessary if you move the jquery library and the app.js file into another directory within the root rather than where there are in this example, local to index.html?
I have tried all of the tricks and nothing appears to work.
Peter
Try changing to
But to make this work, the folder has to be in same place as your html file.
The code got formated :/
It should be: try changing <script src=”app.js”></script> to <script src=”folder/app.js”></script>.
^ as stated this solution doesn’t work. If I leave the file local to index.html, then the code executes and all works correctly.
However, if the file is moved to another folder, the code does not execute properly. True, the function is called, therefore the app.js is found in its new location; however, the response from malier.php is not passed back to index.html
I think that the problem might have something to do with passing the page name “mailer.php” through the function. please test it there and see if you don’t get the same result.
I’m trying to get AJAX to work, but the emails on my contact form do not go through. This error message comes up in the console:
POST 500 (Internal Server Error)
m.ajaxTransport.send @ jquery-1.11.3.min.js:5
m.extend.ajax @ jquery-1.11.3.min.js:5
(anonymous function) @ main.js:79
m.event.dispatch @ jquery-1.11.3.min.js:4
m.event.add.r.handle @ jquery-1.11.3.min.js:4
The “fail” function in my jquery causes the error message to show up in the formMessages div. So it looks like ajax is responding, but doesn’t send out the email…any idea why?
Hi Jessica,
Do you know which version of PHP you are running?
It sounds like a problem with the mailer script.
I tried running it on my website, online.
Jessica – the tutorial uses JQuery 2.1.0 while you are using 1.11.3
Try changing to 2.1.0, or if you are using 2.1.0, it could be you are loading two versions of JQuery.
Hi Matt, thank you for the ajax tutorial, can you plz give a link to full code?
Thanks for reading!
You can download the full code here: http://cl.ly/290k070a1m1Z
Any advice on how to add a loading animation/GIF as the form is submitting? I have some forms that are taking about 10 seconds to post, before the success shows up. Would like to add an animated GIF next to the submit button to let the user know the form is still submitting. Thanks!
Hey Jon, Try This . . .
———
// Submit the form using AJAX.
$.ajax({
type: ‘POST’,
url: $(form).attr(‘action’),
data: formData,
beforeSend: function(){
form.prepend( formMessages.html(‘ Email is sending…’).fadeIn() );
}
})
———-
Where “beforeSend” creates your spinning image using Font Awesome’s spinner icon and spin function. This message is created inside the formMessages
I had the same issue, so thought I’d share my solution.
replace the $ajax{} in the app.js file to:
$.ajax({
type: ‘POST’,
url: $(form).attr(‘action’),
data: formData,
beforeSend: function(){formMessages.text(‘Email is sending…’).fadeIn();}
})
im getting, error message : Oops! Something went wrong and we couldn’t send your message. seems like it fails at : ” if (mail($recipient, $subject, $email_content, $email_headers)) { … }”
I am trying to learn this method. Hope you can help.
Previously i received the error due to that status code. and i commented it. Any suggestions ?
Hi,
i’ve read, that on some shared hosts you might encounter problems with PHP mail function if you do not use its fifth additional_parameters with the “-f” option. Your code then could look like
if (mail($recipient, $subject, $email_content, $email_headers, “-f mail@yourdomain.com“)) { …}
where in some examples in the web there was no space behind the “-f”…
Check the PHP documentation for more explanation. Hope this helps.
Great tutorial, thank you, it also easy to use
I get this error even though the mail get sent:
Fatal error: Call to undefined function http_response_code() in /nfs/c08/h02/mnt/116936/domains/gimme-a-gig.com/html/mailer.php on line 40
Any solutions to this? I am clueless when it comes to PHP ;o)
Hi Glenn,
Looks like your version of PHP doesn’t support the http_response_code() function.
Try setting the response code manually. For example:
header(“HTTP/1.0 404 Not Found”);
More info: http://uk1.php.net/manual/en/function.header.php
Let me know how you get on
Hi Matt
Thanks for the help – I managed to use the latest version of PHP and it worked. I am a novice when it comes to PHP. Does header(“HTTP/1.0 404 Not Found”); replace http_response_code(200); in your solution? Just curious…
Good stuff! 🙂
Yep, the header() function would replace http_response_code().
Matt, you rock!!!
Although, for some reason, my success and error messages show up in a new page. I have my “form-messages” div at the bottom, next to my submit button, could that be the culprit?
Once again, thanks for this!
Hi Reme,
This sounds strange. Would you be able to share your code with me?
I’d be happy to take a look and see what’s going on 🙂
Hey Matt,
How would you want me to share it with you? On here or by email?
Appreciate your help!
Hey Matt,
I’ll gladly share my code with you. How would you want me to?
On here or by email?
Appreciate your help!
Happy to do either 🙂
You can reach me at matt.west [at] kojilabs.com if you prefer email.
Sent you an email.
Thanks for your help!
Much appreciated 🙂
Hi Matt,
Even i am having the same issue, the mail is sent and the response is going to a new page.
Can you help me with it.
Thanks & Regards,
Vishal S
First of all, thank you for the great tutorial!
I am having the same issue of being redirected to a new page on success and failure. Did you find a solution for Reme? If so could you post it here?
I would also be very grateful if I could send you an email with my code for you to take a look, with your permission of course.
Thanks for the tutorial, Matt!
I’m having one issue, though. While the email is sending correctly, the error message from within the fail() call in app.js is showing after sending (I ensured the wording differed in each error message across the JS and PHP files so I could tell which error was which). I verified that my syntax matched yours, and the error persists. This happens in Chrome 33 and Firefox 24. I am using PHP 5.3.26, so does it have something to do with mailer.php’s http_response_code() not working in < v5.4, as AJ Foster commented?
Hi Alex,
Yes, I suspect that’s what’s causing the problem.
Try setting the response header manually rather than using http_response_code().
Here’s an example:
header(“HTTP/1.0 404 Not Found”);
More info: http://uk1.php.net/manual/en/function.header.php
The status codes and names can be found in the code comments 🙂
Let me know how you get on.
The overall technique is nice and clean and tidy, but the backend code is deficient. It is weak to many forms of injection attacks. Using:
$email = filter_var(@$_POST[’email’], FILTER_SANITIZE_EMAIL );
As the “from” email is specified in the header field of the mail function, commas and ; and other stuff can be injected to make the SMTP server do all kinds of weird things.
The PHP world is full of a lot of noobs that cut and paste scripts they find from Google searches that are full of nasty security holes.
Hi Aaron,
Thanks for the feedback.
Admittedly I’m not much of a PHP guy but I wanted to provide some idea of how this would work on the server-side rather than just calling a non-existing file.
You have a valid point about people copy and pasting this script for use in their own websites.
I’d be happy to update the post if you can provide a more secure version of the mailer script.
Thanks!
Matt
Here you go:
https://gist.github.com/AaronTraas/9533041
It’s easy to get yourself in trouble not knowing what you’re doing in PHP because PHP is so easy to pick up and find sample code for. I know because I’ve learned the hard way.
Thanks Aaron!
Tested, updated, and credited 🙂
Much obliged!
Note: I didn’t test the code; I’m at work and don’t have time for that. I’d recommend testing before posting.
I am getting a 500 error response code, even w/ the default files attached.
Any thoughts?
Hi Travis,
Which version of PHP do you have installed?
As AJ pointed out earlier in the comments, the mailer script will require version 5.4
If that’s not it, check your log files to see what error is being raised. Post the error message here and I’ll try to formulate a solution.
Hey Matt, do you by chance do development work per hour? If so, can you contact us? Need help with this and more.
It’s useful thank you, it also easy to use
NICE !!
What is the benefit of this method over using direct php post methods?
It allows you to submit form, without page reload. 🙂
Sounds cool.
Thanks 🙂
Minor note: http_response_code() requires PHP >= 5.4, which is not necessarily standard on all servers. The current Ubuntu 12.04 LTS package, for example, has 5.3.
Thanks for picking that up AJ 🙂