Starter Kit : 24 Business Card Templates - 6 Logo Templates - 2 PSD Templates - 42 Stock Photos

Designing a stylish contact form for your portfolio

July 12, 2008CSS HTML64 Comments


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.

  • http://www.logodesignpros.co.uk John Petrucci

    Good.. I really appreciate.

  • http://developerfox.com Fox

    Cool, I saved it.

  • http://blog.cfdesignz.co.uk Craig Farrall

    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

  • http://www.newbeginnings.nl Marcel

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

  • http://www.crazyleafdesign.com Alex

    @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.

  • http://www.newbeginnings.nl Marcel

    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

    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?

  • http://www.crazyleafdesign.com Alex

    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

    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!

  • http://www.crazyleafdesign.com Alex

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

  • Sarah

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

  • http://www.crazyleafdesign.com Alex

    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

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

  • http://www.crazyleafdesign.com Alex

    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).

  • http://www.argentinafly.com/contact.html arturo

    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

  • http://www.kameli.net/~kimmo/ Kimmo

    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.

  • http://www.crazyleafdesign.com Alex

    @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.

  • http://www.shabbirhossain.com Shabbir

    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

  • http://brazilspeaksportuguese.blogspot.com Angie

    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

  • http://www.crazyleafdesign.com Alex

    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

    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

  • http://www.crazyleafdesign.com Alex

    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

    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

  • http://www.crazyleafdesign.com Alex

    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

    Alex, you rock! thanxs

  • Angie

    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

  • http://www.ifixurpics.com Neateye

    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

  • http://home.myfairpoint.net/jontman Jon

    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

  • Pingback: 5 Different Ways To Create Stylish Contact Form Layouts | Minervity

  • http://www.daniel-sharp.co.uk/contact.html Dan

    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! :)

  • http://www.crazyleafdesign.com Alex

    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.

  • http://www.danel-sharp.co.uk Dan

    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.

  • http://www.crazyleafdesign.com Alex

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

  • Greg

    Alex, how do you get the form to send the “success” message back to the page that the contact form is on? I do not want to go to a thankyou.html page. I would like to put the “success” message right back on the same page under the contact form itself without having to go to a different page.

  • http://www.crazyleafdesign.com Alex

    Greg … the form is such created that it forwards the user to another page using PHP’s header function. I would have to think about your request and come up with a solution.

  • http://pix-webdesign.com/ CSS Webdesign

    it was a very useful for my works! :) Thank you!
    10 by 10!!!

  • http://www.anandshenoydesigners.co.uk Anand

    Hi,
    Firtsly, Thanks for the stylish form. It looks really gud.

    I have just finsihed uploading my website onto the server which also contains the contacts page that has the form. I seem to have problems recieveing mails even though I have tried using 3 diffrent mailto IDs. The mail IDs I have tried are info@anandshenoydesigners.co.uk, shenoy.anand@gmail.com, ana_arch@yahoo.com. Please help!

  • http://www.crazyleafdesign.com Alex

    I’ve uploaded the contact.php file we are using on our website and it’s working. You can download it from here : http://www.crazyleafdesign.com/contact.zip . You just need to personalize the file with your email and text.

  • J

    Hi
    I haven’t uploaded my contact form yet, do I have to do that for the .php file to work?
    Because when I click on the ‘send’ button the contact.php file comes up, the actuall codes, not the ‘thankyou.htm’ file… What can be wrong?
    Thanks

  • J

    Hello again!
    Now I have enabled php with MAMP and when I test the contact form and write in every line and click on the “send” button it all just gets erased and I don’t get the email!!
    What should I do, can I perhaps send you a zipped file with my files so you can point out the error?
    Thanks

  • Ryan

    what do I name the css file? I couldn’t find this information on this tutorial.

    Thanks!

  • http://www.crazyleafdesign.com Alex

    @Ryan … You can name the CSS file any way you like. Just include in the HTML file hosting the form this line : <link rel="stylesheet" type="text/css" href="The_Name_You_Like.css" />

  • James

    Hi, thank you for designing this form. Good job. I’m trying to use it, but I keep getting this come up when i click submit… what am i doing wrong?!

    \r\nReply-To: \”$name\” \r\nX-Mailer: chfeedback.php 2.04″ ); header( “Location: $thankyouurl” ); exit ;

  • James

    It’s ok! Got it working… I was testing it offline, and so the url value was wrong… silly me!

  • Joanna

    I am trying to move the boxes over for certain parts of the form so it’s not a long list of items to fill out and they are more like two or three next to eachother: city, state, zip all next to eachother but with the label text for each above them…

  • Joanna

    And I’d also like to add option buttons for Yes or No..

  • http://www.pictureworldbd.com era

    using contact form when i send message then it cannot go thanyou page. It give a message
    ” Warning: Cannot modify header information – headers already sent by (output started at /home/shahid/public_html/contact.php:9) in /home/shahid/public_html/contact.php on line 38″

  • http://www.pictureworldbd.com era

    using contact form when i send message then it cannot go thankyou page but i created contact.html,contact.php,thankyou.html,error.html. It go to contact.php file and delivered a message which is given below:

    ” Warning: Cannot modify header information – headers already sent by (output started at /home/shahid/public_html/contact.php:9) in /home/shahid/public_html/contact.php on line 38″

    pls tell me what will i do?

  • http://www.pictureworldbd.com era

    I found problem my contact.php file. I solved this problem.
    Thx for your script

  • Sam

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

    I am testing with this tutorial for a form on this website.

    But it just wouldn’t work!! :-(

    I tried using different email addresses in the mailto: section… …@gmail.com, …@hotmail.com,…@rediffmail.com

    but not once did the email go through on clicking send on the form

    PS: i havent created the thankyou and error html files, though i have mentioned their paths in the php file. could that be the problem? but the script works so far as trying to open them correctly as expected. But no mail is going thru

    Please help!

  • PMN

    Hey,

    How to get feedback form data into my server.
    I need entire form input details into my server as EXCEL file.

    Help me..

    Regards,
    PMN

  • http://www.chocolatesbybenoit.com Nico Nicomedes

    Hi Alex,

    I tried using the form you provided, following each instructions carefully. I believe I never missed out anything but the form still does not function properly. You can check the form I created using the tutorial you provided at http://www.chocolatesbybenoit.com/christmas.html.

    The problem is whenever I click send, without entering any information, it just loads back to the $formurl location. I have both error.html and ordersuccess.html created as well.

  • http://www.patrickvalmont.com patrick

    Thanks for sharing this post. I already have a contact form a bit like this one, but with no color scheme like yours. I will test this soon as I get home. I kinda like the hovering in the form fields. looks nice. thanks

  • http://www.patrickvalmont.com patrick

    yep, it works fine. no problems whatsoever. thanks

  • http://www.benstokesmarketing.co.uk Ben Stokes

    Thanks for the post, the script looks really simple as well. I may try this on some of our new projects.

  • http://www.r0gu3.com Lee

    Hi, I have had a go at this but i receive this message once the send button has been clicked:

    Warning: Cannot modify header information – headers already sent by (output started at /home/r0g47637/public_html/contact.php:8) in /home/r0g47637/public_html/contact.php on line 42

  • http://AHdesignsonline.com Ashton

    Hi. Thanks for this tutorial. I really like it but i am having some trouble. Everything works perfect except i never actually receive any emails. Any help that you can give me on this would be greatly appreciated. You can see it on my site at http://www.ahdesignsonline.com/cleanweb/contact.php

    PS. I haven’t styled it yet cause i want it to work first

  • http://www.crazyleafdesign.com Alex

    Hey Ashton. Try do download the files we are using on our website and just modify them to suit your needs (your email, subject etc). The download URl is here : http://www.crazyleafdesign.com/contact.zip

  • http://AHdesignsonline.com Ashton

    Hey thanks. i downloaded your one but still no change. Everything acts like it works but it never sends an email. I feel like i have tried everything.

  • http://www.crazyleafdesign.com Alex

    Ashton, send me your contact.php file you are trying to use on your website to this email address : crazyleafdesign.com [at] gmail.com

  • http://www.glenhealy.com Glen

    I am having the same issue as ashton. I never receive an email. Can someone help me out?

  • http://pulse.yahoo.com/_SZBWFUWIAMEROMKD44T34Q3HCI dimitra

    hi

  • http://pulse.yahoo.com/_SZBWFUWIAMEROMKD44T34Q3HCI dimitra

    hiiii

  • dimitra dimitra

    hiiiii

  • dimitra dimitra

    kkkk

© 2013 CrazyLeaf Design Blog. All Rights Reserved. Proudly maintained by Alex Ionescu and Bogdan Dascal.