Templateo - Website Templates

Designing a stylish contact form for your portfolio

by Alex
Posted in CSS, Graphic Design, HTML, Programming on July 12th, 2008

This tutorial will show you how to design a stylish, custom contact form for your website / portfolio from scratch. It will cover all the aspects of the design including the CSS and HTML coding and PHP programming.

The result will be a fully functional, personalized contact form and will look like this :

Designing a stylish contact form final result

As you can see the contact form consists of 2 parts : one containing the contact information of the sender, labeled “Your contact information” and one part regarding the sender’s message, labeled “Your message“. Each part represents, as you will see below, a fieldset tag, and both are integrated in a form tag. The form also integrates a very simple and basic captcha system that you will see works pretty well, considering most spammers aren’t concerned about portfolio websites, so they don’t use advanced spam robots to trick these contact forms. A more advanced captcha system can be developed using a database and an array in PHP or can be found can be found on the internet (for example here).

The HTML part

For the HTML part you will need some basic knowledge of some HTML tags like : form, fieldset, label and input. As I said before the form is composed by 2 fieldsets. The first fieldset, “Your contact information“consists of 3 text inputs, namely “Name“, “E-mail“, “Subject“. As you can see in the code below we are using labels to name the inputs and p tags to align them 70px to the right (this can also be done via the CSS file).

The most important thing regarding the input tags is their name attribute. We named each input according to what it does, “name“, “email“, “subject“. Remember these as we will use them in the PHP file.

The second fieldset contains the message box defined by a textarea tag with 7 rows and 50 cols (these are attributes that define the textarea’s height and width) . It also includes the input for the captcha and a special kind of input, for the submit button. For this fieldset too it’s important to remember the name attributes of each input and textarea, namely “comments“, “spam” and “submit“.

<form action="contact.php" method="POST">
<fieldset>
<legend>Your contact information</legend>
<p style="margin-left:70px">
<label><strong>Name:</strong></label>(required)<br />
<input type="text" name="name" value=""  />
</p>
<p style="margin-left:70px">
<label><strong>E-mail:</strong></label>(required)<br />
<input type="text" name="email" value="" />
</p>
<p style="margin-left:70px">
<label><strong>Subject:</strong></label>(required)<br />
<input type="text" name="subject" value="" />
</p><br />
</fieldset>
<fieldset>
<legend>Your message</legend>
<p style="margin-left:70px">
<label><strong>Message:</strong></label>(required)<br />
<textarea name="comments" rows="7" cols="50" ></textarea>
</p>
<p style="margin-left:70px">Security code : <strong>56728</strong><br />
<label><strong>Please enter the code you see above :</strong></label>(required)<br />
<input type="text" name="spam" value="" />
</p>
<p style="margin-left:70px">
<input class="button" name="submit" type="submit" value="Send" />
</p>
</fieldset>
</form>

As you can see the form calls a file called “contact.php“. This is the PHP file that we’ll actually use to send the email message.

The CSS part

We created the HTML form. It’s time to dress it up a little. We”ll do this by defining proprieties for the inputs and textareas in the CSS file.

As you can see we defined 3 proprieties for the inputs and textarea, one in the normal state, one on hover (when the mouse is over it) and one on focus (when you actually click on it and start typing text). The hover and focus states are identical in this case, but you can define them individually. As you can see when we hover over the inputs and textareas this image is loaded in the top right corner. Also the color of the border changes from #666666 to #000000. There are virtually unlimited possibilities on what you can in terms of style with these textareas and inputs and depends only on one’s creativity.

input, textarea {
border:1px solid #666666;
font-family:Verdana,Tahoma,Arial,Sans-Serif;
font-size:1em;
margin:0;
padding:4px;
background:#fff;
}
 
input:focus, input:hover,textarea:focus, textarea:hover{
border: 1px solid #000;
background:url(../images/contact_crazyleaf.gif) top right no-repeat;
}
 
label {
margin:2px;
}
 
input {
width:300px;
}
 
.button {
margin:0 0 15px 0;
background:url(../images/div_back_contact.gif);
color:#000;
font-weight:bold;
width:310px;
}

Minor alignment adjustments are also a must so everything aligns perfectly. The contact form is the last thing standing between you and the client. You want it to be as perfect as it can be.

The PHP part

This is the really tricky part.

First off you need to create a file (with Notepad or other text editor) called contact.php You can name this file anyway you like, just remember the name of the file must be identical to what you input in your action attribute on your HTML form. Additionally you have to create 2 more HTML files, one called thankyou.html, and one error.html. The first file will be loaded if the information from the form has been send successfully and the second one in there were errors.

The PHP file consists of multiple parts. The first part defines some constants, like the email address the information will be sent to ($mailto), the URL that contains the HTML form that is collecting the information ($formurl), the URL of the thank you page ($thankyouurl), and the URL for the error page ($errorurl).

The second part reads the information sent by the HTML form. Remember when I said it’s important to remember the name attributes of each input and textarea? Well, this is where we use them. This part of the script takes each value inserted in each input and textarea and inserts it into a PHP variable.

The third part verifies with two PHP if instructions if the required fields have been completed and if the basic captcha has been entered correctly. If so the script passes to the final part, if not the error page will be displayed.

The forth and last part of the script composes the message and sends it to you (to the mail address defined in the first part of the script, in the configurable area).

<?
$mailto = 'name@name.com' ;
$subject = "Contact form message - Crazyleafdesign" ;
$formurl = "http://www.yoursite.com/contact.html" ;
$errorurl = "http://www.yoursite.com/error.html" ;
$thankyouurl = "http://www.yoursite.com/thankyou.html" ;
 
// -------------------- END OF CONFIGURABLE SECTION ---------------
 
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$subject = $_POST['subject'] ;
$comments = $_POST['comments'] ;
$spam=$_POST['spam'] ;
$http_referrer = getenv( "HTTP_REFERER" );
 
if (!isset($_POST['email'])) {
	header( "Location: $formurl" );
	exit ;
}
if (empty($name) || empty($email) || empty($subject) || empty($comments) || $spam!="56728") {
   header( "Location: $errorurl" );
   exit ;
}
$name = strtok( $name, "\r\n" );
$email = strtok( $email, "\r\n" );
$subject = strtok( $subject, "\r\n" );
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}
 
$messageproper ="---------- Crazyleafdesign.com message ----------\n\n" . "\nSent by : " . $name . "\nEmail : " . $email . "\nSubject : " . $subject . "\n\n\nMessage : " . $comments;
 
mail($mailto, $subject, $messageproper, "From: \"$name\" <$email>\r\nReply-To: \"$name\" <$email>\r\nX-Mailer: chfeedback.php 2.04" );
header( "Location: $thankyouurl" );
exit ;
?>

That’s about it. The form is complete. You can see it in action on our site.

A custom contact form with an upload field will be available in one of our future tutorials.

Share this article
IzzyWebsite - CMS - The Easiest to Manage, Content Management System!
33 ResponsesLeave a comment
  • John Petrucci
    July 15, 2008 at 12:03 pm

    Good.. I really appreciate.

  • Fox
    July 17, 2008 at 4:11 pm

    Cool, I saved it.

  • Craig Farrall
    July 18, 2008 at 12:14 pm

    Nice contact form, looks really good, I think you should have another post adding abit of ajax to it so it can give you suggestions, I think that would be a good read.

    My last blog post : Wordpress 2.6 available

  • Marcel
    July 21, 2008 at 10:47 pm

    Somehow it doesn’t work for me even though I followed instruction to the letter

  • Alex
    July 21, 2008 at 11:35 pm

    @Marcel … I just sent you a message using the contact form on your website. It worked like a charm. Let me know if there is anything else I can help you with.

  • Marcel
    July 21, 2008 at 11:41 pm

    I got it working now.

    Only sometimes I get the message ‘No input file specified’
    I will figure it out.

    Thanks for the code it looks very nice !

  • DarkShadow
    August 13, 2008 at 7:27 am

    HEy i have a problem!!

    Warning: Cannot modify header information - headers already sent by (output started at /home1/justacar/public_html/contacto.php:2) in /home1/justacar/public_html/contacto.php on line 34

    ??¿?¿?¿

    Whts wrong?

  • Alex
    August 13, 2008 at 12:05 pm

    Usually this error is caused by empty white spaces between php start and end tags. You can see here a list of possible problems : http://www.tech-recipes.com/php_programming_tips1489.html . If you still can’t make it work let me know.

  • Sarah
    August 26, 2008 at 5:06 pm

    I am trying to use this html snippet, but I can’t figure out if it is possible to have the contact form sent directly to my email without opening the user’s mail program. Your help is appreciated!

  • Alex
    August 26, 2008 at 7:21 pm

    That’s what this article is about. It explains how to make a contact form that sends visitors messages to your inbox.

  • Sarah
    August 26, 2008 at 7:43 pm

    Right, but does it open a mail program? And am I able to redirect users to a thank you page?
    Sorry for the confusion!

  • Alex
    August 26, 2008 at 8:32 pm

    No problem. It doesn’t open a mail client. It just sends the mail and then redirects the user to a thank you page. You can test it from a “sender” point of view here : http://www.crazyleafdesign.com/contact.html

  • Sarah
    August 26, 2008 at 9:29 pm

    Oh got it now! Thank you. Last question I promise, where do you save the contact.php file?

  • Alex
    August 27, 2008 at 1:36 am

    No problem, Sarah. You can ask as many questions as you like. That’s why we are here. The contact.php file is saved in the same folder as the contact.html file (the file containing the HTML form).

  • arturo
    October 10, 2008 at 9:02 am

    dear alex,

    thank you very much for this script. it was very helpful for me due that I am inexpert in this matter. it works perfeclty.

    best regards from buenos aires

    arturo

  • Kimmo
    October 17, 2008 at 11:05 pm

    Hi!

    This may seem like nitpicking, but I’d rather be safe than sorry with these things. It would be safer not to use the visitor provided e-mail address as the reply-to address without validation. The provided address may not be valid which may result in the SMTP server refusing to send the mail. Instead, you should try trimming off the preceding and trailing whitespace of the provided e-mail address and validating the resulting string against a regular expression. If the address passes validation it’s safe to use as a reply-to address.

    You should also include some metadata, like the sender’s ip address, into the messages and - just to be sure - store the whole form data on the server with timestamps and ip addresses. Also, you need to make sure you handle all possible error situations and check every major operation for errors. You never know what may go wrong.

  • Alex
    October 17, 2008 at 11:47 pm

    @Kimmo … You are absolutely right. This tutorial was focused more on the “design” aspect of the contact form and provides a basic contact method for your website. For a more advanced approach you must use some kind of validators and spam protection. I’m working on a version which will solve all these aspects.

  • Shabbir
    January 15, 2009 at 12:00 pm

    Hi,

    Thanks for the excellent tutorial. But somehow it didn’t work from my end. Everything is working fine. I meant if I keep empty field then it goes to the error page and if I put the right thing then it goes to the thank you page. So the condition is working fine. But I dont get e-mail. I have used my gmail Id for “$mailto = ‘……@gmail.com’ ;”.

    Can you please tell me what could be the problem? I am not so good in PHP. so your help will be highly appreciable.

    My dev version is under:
    http://shabbirhossain.com/dev/mail

    Thanks,
    Shabbir

  • Angie
    January 27, 2009 at 10:43 am

    Designing a stylish contact form for your portfolio

    Hey guys,

    This is awesome! But guess what ? I can’t get it to work. I tried send a message and what I get is ” The blog you were looking for was not found” Maybe the stylish contact doesn’t work on blogs?? Would you be able to help me out?

    Thank you guys

    A

  • Alex
    January 27, 2009 at 12:23 pm

    Hey Angie,

    The contact form wasn’t designed with blog usage in mind. It was made for a normal website. Let us know what blogging platform do you use and we’ll see what we can do about it.

  • Angie
    January 27, 2009 at 4:24 pm

    Thanks Alex,

    So I’m currently using blogger.com. The form showed up beautifully on the page, but as I said running a trial to make sure the form worked it sent me to a no show page. Blogger anables you to use java and html tags.

    Thanx again

  • Alex
    January 27, 2009 at 5:45 pm

    You have to create a thankyou.html and error.html page, upload them to your website and then modify in the contact.php file $thankyouurl and $errorurl variables point to the location on your server where the thankyou.html and error.html files are.

  • Angie
    January 28, 2009 at 12:57 am

    Hmmm, I don’t know. I have these ones made up already. Maybe I should send you the php, css, html codes I copied from you plus my add ons??? WHat do you think? I can’t really see were the mistakes are. : ( Thanxs

  • Alex
    January 28, 2009 at 9:59 am

    Hey Angie. Archive all the files into a zip archive and send them using our contributors form here : http://www.crazyleafdesign.com/blog/contributors/

  • Angie
    January 28, 2009 at 5:14 pm

    Alex, you rock! thanxs

  • Angie
    January 29, 2009 at 8:54 pm

    Hey Alex, trying to get the file into a zip archive has been a hassle, so here you have the url (below). Maybe I have html, css, php mistakes and that’s why isn’t working on the blog. Who knows. What’s wrong if them? Thanks

    http://docs.google.com/View?docid=dfqkkw99_36gpspj6fd

  • Neateye
    April 1, 2009 at 1:35 am

    Hi Mate… thanks for the info on ‘contact forms’, works well with my site. cheers. Was wondering if you can help with an ‘upload-input’ form. one that will be relatively safe for me and my server? is it possible? id much appreciate if you can drop me a msg. Thank again.
    Neateye.
    ifixurpics.com

  • Jon
    April 7, 2009 at 3:07 am

    This was exactly what I was looking for! In fact it was a little better. When I put the edited the scripts and put them on my site, everything worked perfect except the css that makes your example look so good. What do I need to do to get the css to recognize and influence how the HTML code renders the form. Thanks,

    Jon

  • Dan
    June 2, 2009 at 3:40 pm

    Hello,

    Firstly thanks for this tutorial - it works really well and was so easy to set-up.

    Couple of questions though if you don’t mind!

    1) As you can see on the URL, my submit button is really wide - how do I make this ‘normal’ button size?

    2) If you fill in the form and press send, it shows my html page (thankyou.html) as successful, however I am still not recieving messages to my email address.

    (I have this line in my PHP page)
    $mailto = ‘danielsharp469@btinternet.com’ ;

    But it doesn’t seem to work. Help please! :)

  • Alex
    June 2, 2009 at 4:46 pm

    Hey Dan,

    1) In your style.css file seach for the class “button” and add a property : width : 100px; or whatever you would like to width to be.

    2) I have sent you an email with the contact.php file we are using for our website.

    Let me know if it’s working now.

  • Dan
    June 9, 2009 at 11:46 am

    Hi.

    Thanks for your reply. I’ve tested it again with the email address and it won’t go through, so that will be about the 50th time I’ve tried it.

    I then tested it with my hotmail email and it arrived within a couple of seconds - so it looks like their is a problem with my btinternet email, not the form! I will have to look in to it.

  • Alex
    June 9, 2009 at 11:53 am

    I understand. I had a few problems with Yahoo Mail also. Glad to see it’s not the form :)

Add a commentGet a Gravatar

* Name

* Email Address

Website Address

You can usethese tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Sponsors
PSD to HTML
PSD to HTML
Elegant Wordpress Themes PSD to HTML
Favicon Ads
Share your design news Free Joomla Templates Free mediawiki skins Free pligg templates Flash web templates Free icons for download CLD Vectors - Free Vectors for Download Free Vectors
RSS Feed
Around The Site
Sponsor
The World's Best Web Design
RSS Latest on zaBox
Sponsors
User Links Panel
RSS User Links
  • Web Design Trend Hunting - Fluid Grid Layout: 10+ Outstanding Examples & 1 Tutorial July 3, 2009
    In our daily web hunting raids we have spotted another interesting & good-looking trend - the fluid grid-based layout
    Web Design Trend Hunting - Fluid Grid Layout: 10+ Outstanding Examples & 1 Tutorial
  • 55 Really Creative And Unique Blog Design Showcase: Inspiration July 3, 2009
    There are millions of blogs around the world, so it's really a challenge to stand out with unique design. Get inspired, to help you create really unusual design!
    55 Really Creative And Unique Blog Design Showcase: Inspiration
  • Design a wordpress theme with Photoshop July 3, 2009
    If you want to create your own custom blog design follow this easy tutorial to learn how to design a wordpress theme in Photoshop
    Design a wordpress theme with Photoshop
  • 4 Useful Random Topics For Bloggers July 3, 2009
    A roundup post for June 2009 for TutZone :)
    4 Useful Random Topics For Bloggers
  • Mark Prints for Self on Other's Blog July 3, 2009
    For a change, this post is for readers from all niche, segment, interest, community and category. Even if you have no relation with the world of web designing you shall find reading it worthy.
    Mark Prints for Self on Other's Blog
  • Web Standards July 3, 2009
    Using web standards improve the accessibility of the websites, reduce development and maintenance time, improve search engine rankings and can also improve accessibility.
    Web Standards
  • 50 Minimalist Websites Design: Best Since 2007 July 3, 2009
    Minimal Websites are interesting because it's a way to let visitors focus on the content. We just mixed the best of Minimalist design featured on CssLeak since 2007. There is 50 websites order by popularity.
    50 Minimalist Websites Design: Best Since 2007
  • HP Workstations - Prize winning video July 3, 2009
    Viral video promoting the HP workstation’s and their ability to bring peoples ideas to life.
    HP Workstations - Prize winning video
Friends
We recommend
Sponsor

Money Saving Tips
Find cheap business cards printing, postcard printing and brochure printing by searching for online coupons. Search terms like "print flyers coupon code" in your favorite search engine.