You are here: Home » All Posts » Contact Forms » Creating a contact form for your website

Creating a contact form for your website

in Contact Forms

Here is a 'ready to use' form that you can simply plug-in to your web site. All that you have to do is to download the code below, update the code to include your email, and attach form to your web site.
You can easily customize the form for your needs. The code is 100% validated XHTML1.0 strict.

Features of this contact form

  • Standards compliant, accessible form
  • 100% XHTML 1.0 strict validated, uses CSS2
  • Secured against spam and attacks
  • Validations done both on client side and on the server side
  • Uses PHPMail to send email
  • Easy to customize
  • Hosted on your own web server
  • Free!

Download the contact form code

You can download the contact form that best fits your needs and then customize as required.


Simple contact form with three fields: Name, Email and Message
Try the DemoDownload
Contact form with simple captcha
Contact form with Simple Captcha
Try the DemoDownload
contact form with image captcha
Contact form with fields: Name, Email, Phone and Message (and Image Captcha)
Try the DemoDownload

Installing the contact form on your web site

  1. Unzip the downloaded contact form code.
  2. Edit contactform.php and edit the email address to your email address
    $formproc->AddRecipient('name@your-website.com');//<<---Put your email address here
  3. Upload the folder contents to your website (to a sub-folder say, /contact)
  4. The contactform.php contains the form. You can link to this file or embed the form using iframe.

Sample iframe code to embed the form:

<iframe src='/contact/contactus.php' frameborder='0' width='100%' height='600' ></iframe>

Alternatively, you can modify contactform.php to have the same look of your web site.

Customizing the form

The contact form can be customized to match requirements. You can change the style, add more fields, and add more email recipients.

Changing the colors, fonts and style

You can edit the contact.css file to change the style of the form.

Adding more than one recipients

Call the AddRecipient() function passing the email address of the new recipient.Example:

$formproc->AddRecipient('name_1@your-website.com');
$formproc->AddRecipient('name_2@your-website.com');

Adding more fields

You can add any type of additional fields to the form. The fields will automatically be included in the form submission emails.

More Customizations …

For more features and customizations of the contact form, see the Related Pages section below.

License


The code is shared under LGPL license. You can freely use it on commercial or non-commercial websites.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

No related posts.

{ 63 comments }

Dave S April 22, 2011 at 1:36 am

Sorry to ask for help but I’ve got the contact form up & it’s looping on the simple captcha . . . thought I’d caught it by matching the keycode from both the contact form & the simple captcha routines but that didn’t work. If you’ve got time to help, it would be appreciated. Thanks!

Thomas M August 18, 2011 at 8:21 pm

I had the same problem then realised that, although it is not made clear, the CAPTCHA image validates against lower case letters only. I have since incorporated code which changes entered text to lower case before it validates against the CAPTCHA.

JLH April 25, 2011 at 4:39 am

I’m having the same issue as Dave S. I currently have this setup on a home web server, and the contact form keeps looping on the captcha after there has been “success” with the captcha code entered. For some reason, it doesn’t not enter the Thank You page. Any assistance would help greatly! Thanks!

Sousdey Yann August 2, 2011 at 4:57 pm

I all so have the same problem as you because when I try  to fill in all information it the box but it doesn’t go direct to web page “Thank ”
Regard, i need for the solution
Thank !!!!

Darren August 18, 2011 at 10:20 am

Having a simular problem. My contact form was working fine until my hosts stopped PHPMail support. Can anyone help with SMTP authentication with this form.
Thanks in advance.

kyle May 7, 2011 at 7:21 pm

I can’t get the captcha image to load, any suggestions?

Deck Hazen May 24, 2011 at 8:19 pm

Hi folks,
I’ve tried to change the font-color to white with “font-color:#FFFFFF;” in every place I could find that looked like it would take it – contact.css and all the PHP files I could find – no luck – I’m not even sure the css file is being read as I can’t even see where it gets called.  Got any ideas, or perhaps a bit more documentation.  I can play with this for another hour or so but if I can’t get it work I’ll have to move on the next “easy” comment package.
-Deck

docj May 29, 2011 at 3:05 pm

Hurrah! I got it to work after like 3 days :-)
I’m using the last form: “Contact form with fields: Name, Email, Phone and Message (and Image Captcha)”.  I’m testing it from my local computer (windows, php installed via a xampp server). If you upload it to a server then you probably need to do different stuff.
First I had to get a mail server working to get php to send emails out from my computer. You can just use the one from your ISP. From tutorial here: edit your php.ini file, changing the line “SMTP=localhost” to e.g. “SMTP=smtp.wlink.com.np”, where “smtp.wlink.com.np” is the smtp mail server of your ISP (look up their website).
Second my emails got rejected because the sender host was not ‘valid’. In the file “fgcontactform.php”, go to the function “GetFromAddress()” and change the line ‘$from =”nobody@$host”;’ (for me this took the value “nobody@localhost”) to ‘$from = “me@example.com”;’. This is a more ‘valid’ host, no clue why :-/
That got the form working for me on my own computer. I haven’t tried uploading the webpage to a server, so I can’t say if there would be problems with that. If anyone has corrections, I would be more than happy to hear them.
@Deck Hazen: I had problems with that too. It wouldn’t update if I went to a different page and came back. What worked for me was pressing refresh on my browser. FYI “contactform.php” calls “contact.css” in this line: “<link rel=”STYLESHEET” type=”text/css” href=”contact.css” />”

Peter Nielsen June 5, 2011 at 10:12 pm

Am using the form with captcha, have installed it on my website. Works fine except when I get the mail from the sender, in the From: field in the email it says the senders name, and here’s the problem part. It says my website in the from field also. Have traced it down to the fgcontactform.php file with this code:
function GetFromAddress()
{
if(!empty($this->from_address))
{
return $this->from_address;
}

$host = $_SERVER['SERVER_NAME'];

$from =”nobody@$host”;  !!!here is the culprit, I tried dropping the nobody and then it was just my website in the from field. I want this to be the senders email address, not my website!!!
return $from;
}
 
Any solutions out there, I am not a php programmer, so if somebody could just tell me what to plot in there so I receive emails it is the senders email address in the from field.
 
thanks Peter

Prasanth June 6, 2011 at 1:50 pm

Email servers properly implementing SPF validation will reject the emails with the user’s email address in the ‘From’ field
See : http://www.html-form-guide.com/email-form/php-script-not-sending-email.html
and SPF validation

Peter Nielsen June 5, 2011 at 10:13 pm

The reply to function is working, that is correct. When I hit reply to in my email program it has the correct sender. But the from field email is my website.
Thanks Peter Nielsen
 

Peter Nielsen June 6, 2011 at 8:12 am

Problem solved, found the place in the code that needed changing, now it works:
 
function SendFormSubmission()
{
$this->CollectConditionalReceipients();

$this->mailer->CharSet = ‘utf-8′;

$this->mailer->Subject = “Kontakt formular fra $this->name”;

$this->mailer->From = $this->email;

$this->mailer->FromName = $this->name;

$this->mailer->AddReplyTo($this->email); !!!changed it from GetFromAddress()-now it works!!!

$message = $this->ComposeFormtoEmail();

$textMsg = trim(strip_tags(preg_replace(‘/<(head|title|style|script)[^>]*>.*?<\/\\1>/s’,”,$message)));
$this->mailer->AltBody = @html_entity_decode($textMsg,ENT_QUOTES,”UTF-8″);
$this->mailer->MsgHTML($message);

$this->AttachFiles();

if(!$this->mailer->Send())
{
$this->add_error(“Failed sending email!”);
return false;
}

return true;
}
 
So thanks. Guess just needed to put it out and woopti, woke up and it solved itself.
 

Prasanth June 6, 2011 at 1:54 pm

Email servers implementing SPF validation will reject the emails with the user’s email address in the ‘From’ field

Peter West June 12, 2011 at 7:07 am

Hi. Great form code. One question how can I add a select field with validation?

Dot July 2, 2011 at 6:30 pm

Please help!!!  I had the form working (sent/received email) but when I uploaded to a website, it won’t work any more.  Performs all the validations but when submitting, comes back to the original form with only some of the fields initially entered still showing.  I’m very confused and don’t even know quite where to begin.  Any suggestions?????
Thanks much!

Richard July 9, 2011 at 4:57 am

Currently also having some issues with changing font colors also, on both contact and thank you page, I can apply font family changes and what not but when i apply any style change to the colors it seems to not accept the color changes.

Richard July 9, 2011 at 6:08 am

http://www.richiesportfolio.com/contact.html

my form is on this page, I have checked all through the files and can’t figure out how the font is staing on black, its driving me nuts :P

Even my thank you page that has not style.css file attached but just some embeded css will only let me change font and font size but the color just stays black always :(

Please any help much apprechiated.

Prasanth July 12, 2011 at 5:16 am

I would suggest to use firebug to trace the font style

cjred July 15, 2011 at 2:29 am

Hi im having this error “Automated submission prevention: case 1 failed” anyone can help me on this?
 

Thomas M August 18, 2011 at 8:41 pm

This message is generated from within fgcontactform.php
 
function Validate()

{

$ret = true;

//security validations
if(empty($_POST[$this->GetFormIDInputName()]) ||
$_POST[$this->GetFormIDInputName()] != $this->GetFormIDInputValue() )

{
//The proper error is not given intentionally
$this->add_error(“Automated submission prevention: case 1 failed”);
$ret = false;
}
 
It is basically saying either that the form’s input ID name was not posted along with the other information from the contact form or that it was posted but it is not the value which the script was expecting.
 
 

oshi August 6, 2011 at 2:59 pm

Hi thanks for the form, i am having a treble trying o add a drop down menu list. i manage to get the code for the html but not php, would be helpful if u can please help me out. thank you

Darren August 18, 2011 at 10:14 am

Hi Oshi,
This worked for me

<label for=’Interested in’ >Interested in:</label><br/>
<select name=”Interested in”>
<option>option1 </option>
<option>option2</option>
<option> option3</option>
<option>option4</option>
<option>option5</option>
</select>
</div>
 

It emails through fine.
One problem I am having now is that my host have now stopped support PHPMail, does anyone know how to add SMTP support to this email form?
 
Many thanks in advance.

Darren August 18, 2011 at 10:15 am

forgot the

<div class=’container’>

has to go on top the code I just posted for drop down menu support

MR September 3, 2011 at 10:21 am

Hi there, i’ve uploaded the form but it doesnt seem to be doing anything? Can anyone please help with this. Thank you.
 
http://www.8pe.co.uk/penny/contact.html

Dalton September 23, 2011 at 2:16 pm

I can’t get the captcha image to show on IIS windows 2003 server. Any ideas? It is the only piece that will not work.

Prasanth September 28, 2011 at 3:36 pm

You need to enable the GD library extension See http://forums.devshed.com/iis-97/iis-6-and-gd-library-149314.html

Dalton September 30, 2011 at 2:04 pm

I have gd2.dll enabled in the php.ini. Also when running the phpinfo() it shows it as fully enabled. Still no image. The same code works on a xampp server.

Hamza Bukhari October 5, 2011 at 4:58 pm

Let me try all this
then i’ll share My experience.

harish October 6, 2011 at 12:34 pm

Awesome

Scott B October 8, 2011 at 6:56 pm

I have added quite a few radio buttons, checkboxes, etc. to my form. The problem I am having is that if one of the required fields isn’t filled out and you submit the form, it comes up with the error message but it erases all the other entries that were previously made, except the required fields. Is there a way to keep that info in place so a person doesn’t have to fill out all these fields again? Please help, I’m still fairly new to PHP and have tried numerous things but none have worked.
Thanks,
Scott

BG daSilva October 20, 2011 at 11:22 am

I just want to say thank you … :)

Comments on this entry are closed.

Previous post:

Next post: