Web Design and Development
Web visitors like feedback. They like to know when a form submission was successful. So its a good idea to display a success message. To do this, we’ll need to make a modification to the initial HTML form code that we showed.
What we’ll need to do is wrap the HTML in a conditional statement which only displays the form if the form has NOT been successfully submitted. Otherwise, it will display some success message. See the revised code snippet below.
<?php
if(!isset($_POST['send']) || $errmsg != "")
{
?>
<form method="post" action="contact.php">
<input type="text" name="name">
<input type="text" name="email">
<textarea name="message"></textarea>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
else{
$success = "<p>Thanks for your message! We'll be in contact shortly.</p>";
echo $success;
}
?>
And that’s it! You’ve just learned how to make a great PHP contact form.
This brief tutorial covered the steps to create a simple contact form. Due to length, the final working code is not posted. But it is available for download here.
You will notice we’ve rearranged a few items, as well as included two versions: pure code and complete HTML page. The Read Me file has additional information which you might find useful.
While this form may prevent spammers from exploiting it to send message to another recipient, it will not prevent them from overloading it with messages to you. One way to cut down on these messages is to require users to confirm their email addresses in a second email field. You could also use a captcha.
Yahoo can be a bit restrictive, so you might have difficulties with this form code.
Fortunately, Carl of Bullseye Property Management was nice enough to troubleshoot and offer one revision to the script. To make this work on Yahoo, you will need to substitute the following line of code:
$headers = "From: $name <$email>\n";
with
$headers = "From: $name <$email>\r\n";
The name variable is optional, however if you include it, it will appear as if it is being sent directly from your inbox. You will also need to enable PHP email within your hosting control panel.
This form code is released under the GPL License. You can redistribute it and/or modify it under the terms of this license. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Tags: email, PHP
Trackback URL for this entry:
http://www.velvetblues.com/web-development-blog/how-to-write-a-php-contact-form/trackback/
Contact us today for a quote. Click here to submit details regarding your project.
If you are making a general inquiry, send an email to info@velvetblues.com
One user asked about adding a drop down box (or select element) to the form. This could be easily done by adding a new element or replacing an existing element with the following sample code:
<select name="gender"><option>female</option>
<option>male</option>
</select>
Then simply modify the$POST variables at the top of the form, as well as the email message to include this variable.
Be sure to edit the select options and name to something suitable for your form.
Thanks for the code, but how exactly should the other modifications be done? Can you be a little more specific? How do you ad the choics that will drop down?
@Carl, you will need to go through 3 steps:
1. Add that select element to the form HTML code. (ie. the HTML code that we’ve shown under the first subheading)
2. Update the $POST variable. (See section 1: Handling User Input). Assuming that you had a select element like the sample above, with the name ‘gender’, your added code might look something like this:
$gender = $_POST['gender'];3. Update the email message. As you will see, we used a single variable for the ‘message’. So we’ll need to combine the variables for your additional element to include that in the message. So you will need to add another line of code after the $gender and $message lines. The code might look something like:
$message = $gender."\n".$message;I have all that working, last question, and thanks for your help…..
In the e-mail I receive from the form the data entered and selected by the user are included, but not the names of the fields they filled out. Example:
Carl Martin
carl@martin.com
Test
This is a test.
SHOULD BE
Name: Carl Martin
E-mail: carl@martin.com
Subject: Test
Message: This is a test.
To fix this, you’d just edit the variable. Using the example that I included in my last reply, you could achieve the following output:
Gender: Male
Message: Some message
using this code:
$message = " Gender: ".$gender."\n Message: ".$message;or, simpler:
$message = " Gender: $gender \n Message: $message";You have been a big help, all of these issues are now fixed. I am having an issue though. You spoke of another way to have the form send the email and I may need you to elaborate on that. I use Yahoo! for my hosting. For some reason, if I use my e-mail address as the address in the form then the mail is sent to me, if I use any other email address…NOTHING!
Yahoo is one of those restrictive web hosts. You will need to consult their documentation, but I am almost positive that you have to (1) use an email address which is set up via Yahoo for your domain, and (2) set it up under the special email ‘PHP Mail Setup’ options, as a valid address from which mail can be ‘sent’…
I haven’t done too much work on Yahoo hosting, so I probably can’t be of any additional help in this regard.
Good luck!
Thanks, I will consult their documentation and their people. I am using the email thats registered and I have done the PHP setup, its just not working, I thought maybe you knew of a workaround. Thanks.
I truly appreciate this script and I have solved the Yahoo! Server issue. I have detailed this below.
In order to get this script to work 100% perfectly with Yahoo! Servers you need to change only a single line of code. This is the original line you need to modify:
$headers = “From: $name \n”;
The modified version should be as follows:
$headers = “From: \r\n”;
The E-mail you actually receive will appear as being sent from Your Email address. To make this something else, Modify the code in this manner:
$headers = “From: FROM NAME \r\n”;
Thanks.
This is not showing up correctly in my post, email me and I will give you the correct data and you can add it, webmaster, thanks.
i sent you an email with the proper code
Thanks Carl, I have added the proper code to the end of the article.
Do you have any code samples lying around for a “Tell A Friend” script that behaves in the same manner as your contact form script?
Hi Carl, no I don’t have a “Tell A Friend” script that I can release. But there are likely tons of them floating around the internet. Google “tell a friend php script“, and you should retrieve some websites which have free samples available.
I did that, most of them do not work property, are cumbersome, or lack features. I love the contact form script and how it works in its own space, no extra windows, etc. Any idea what would have to be modified to make it send data like a refer a friend script? thanks.
Depending on what features you’d want, it will most likely be a bit more complex than this script.
But the simplest possible implementation would be one where you just change the recipient email address and have it point to a user-supplied email address for their friend. And then you could have a standard message that is sent to all ‘friends’…
Great post Velvet.
Very simple and effective php form.
Thanks
Rob
Very simple and effective php form.
great write up
I’m using a local host. I tried and tested it but my yahoo account is not receiving anything.
I changed the email address provided in sendmail.php to my own email but it seems like is not working on my side.
What should I do?
Please help.
@webhostingruching: Perhaps your localhost does not have a working mail server. That is pretty common. What software are you using?
I tried the code and I’m getting an error that says that eregi is deprecated. It’s also saying Notice: Undefined index: message in C:\wamp\www\temporary\contact.php on line 11 I’m seeing this on the localhost, my website doesn’t support php yet.
I tried using your script and I got an error saying that ergeli is depracated and undefined variable on line 11. I’m trying to append this to a site that i’m working on.
Hi
I am using this contact form .But Could you please let me know how to connect to the mail server so that ie sends a actuall email.
Regards
Malar
I have to ask why you automatically assume people know where this sendmail stuff is, I am working with WAMP and I am a newbie but I am trying to crash course PHP. I completely ran the script without an error, but guess what? I have no idea how to actually get the email. Not even the first thing.
Couldn’t you have at least said “Gee, for those who aren’t ZEUS at programming and aren’t born with inherent GODLIKE programming knowledge, there is this thing called sendmail and it has to be specially configured knowing quantum physics!” I am going to quit programming because of this halfway written crap and go clean cars, or maybe burn myself.. thanks a lot.
As I am developing my site I needed to make a contact script for my page, and this is definitely a huge help! Thank you!