¿Cómo crear un formulario de contacto en HTML?

This tutorial will teach you how to create a very simple contact form for HTML based website template.

First of all create 2 files: contact_form.html and contact.php. The first file will contain HTML code for the form and the second -will process the data from the form


HTML

Below is the HTML code of the contact form

<form action="contact.php" method="post">
	Your name
    <input type="text" name="cf_name">
	Your e-mail
    <input type="text" name="cf_email">
	Message
    <textarea name="cf_message">
	<input type="submit" value="Send">
	<input type="reset" value="Clear">
</form>

And this is how it will look in the browser

Let’s have a quick look at some main aspects of it. The <form> tag should have 2 additional attributes:

action=”contact.php” – this attribute specifies where to send the data from the contact form fields, when it has been submitted

method=”post” – this attribute specifies how to send data from the form to the file specified in the action attribute

The <input> and <textarea> tags should have an attribute “name” with a unique identifier. This attribute is used to identify form data after it has been submitted to the server
And the 2 input elements that are used as Submit and Clear buttons, one should have type=”submit” assigned to it and the other type=”reset”

That’s basically it. As easy as it looks

PHP

Now for the contact.php file that will actually grab the data from the fields, compose into a message and send to your email. You can download contact.php file from this link. Below is the code of the file with comments to its major sections.

Assigning the data sent from the contact form fields (cf_name, cf_email, cf_message) to php variables ($cf_message, $field_email, $field_message)

$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];

$mail_to shall contain the site owner email, this is where the email is sent to. You can specify multiple emails by separating them with a comma (e.g. mail-one@template-help.com, mail-two@template-help.com)

$mail_to = 'test@test-mail.com';

Subject of the email you receive from the contact form

$subject = 'Message from a site visitor ' . $field_name;

Constructing the body of the message

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

Constructing the headers of the message

$headers = "From: $cf_email\r\n";
$headers .= "Reply-To: $cf_email\r\n";

Defining mail() function and assigning it to a variable $mail_status, which is used below to check whether the mail has been sent or not

$mail_status = mail($mail_to, $subject, $body_message, $headers);

If the mail() function executed successfully then do the code below

if ($mail_status) { ?>
	<script language="javascript" type="text/javascript">
		// Print a message
		alert('Thank you for the message. We will contact you shortly.');
		// Redirect to some page of the site. You can also specify full URL, e.g. http://template-help.com
		window.location = 'contact_page.html';
	</script>
<?php
}

If the mail() function fails, then execute the following code

else { ?>
	<script language="javascript" type="text/javascript">
		// Print a message
		alert('Message failed. Please, send an email to gordon@template-help.com');
		// Redirect to some page of the site. You can also specify full URL, e.g. http://template-help.com
		window.location = 'contact_page.html';
	</script>
<?php
}?>

You can also download compiled contact_form.html and contact.php files in a single package

 

Proceed to the tutorial on how to add fields to the contact form

¿Cómo crear un formulario de contacto en HTML?, 3.8 out of 5 based on 12 ratings
  • Aya

    Hello,

    1st thanks,
    When I try this code (how to create a contact form), I got the following error:

    Warning: mail() [function.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 D:\XAMPP\xampp\htdocs\Own_Work_Comp\Company_Site\contact.php on line 11
    Failure

    • Chris Diaz

      The contact form script can be used only on a live server. You are using it on your local server (XAMP based).

      In order to make it work you need to upload it to the live server (order hosting) or configure mail server on your XAMP localhost

  • http://www.zooqc.com Bill

    Hi

    your tutorial is very easy understand and viable, but I have a question about how to make contact.php when contact form have a drop down selection. thanks if you can have any help.

    Bill

  • somebody

    I used this form and I like that it is simple but I need to make some fields to be “required” how can I do this using this form?

    • Chris Diaz

      The tutorial on how to create required fields will be added in nearest future

      • somebody

        Thanks, could you post the link for that tutorial on a comment here(when that tutorial will be available)?

  • http://dreagoen.jimdo.com Dreagoen

    Hi, after 10 minutes of trials and errors I managed to do that on the host it’s very simple. I don’t even know how do i made errors at all, maybe i’m just don’t pay enough attention. So if someone is failing at doing this, here’s the tip: Read EVERY WORD in this tutorial… And thanks to Gordon for the tutorial.

  • magnus

    Hi. do i need to have installed a mail server on my web server for this to work? I have set it up like its shown in the tutorial, and i get the “tank you for the message i will contact you shortly”, but no mail to the mail to address. Is there something i`m missing. What are the requirements for this form to work?

    • Gordon J. Blair

      Most local servers do not include mail server, so you’ll need to install some additional software for that. Though there are many risks connected with that, so I suggest you test the form on a live server.
      If you don’t have a host and a domain yet, then google for some free hosting providers. There are many on the web and most of them support php mail() function

  • http://ericloomis.webs.com/ eric

    i need a form box that will email back to my email account. I have the box part which it was easy but i can’t email my account to me. Please help me.

  • http://www.theanimeinn.com Jaswant

    Hi,
    The tutorial is very easy. Thanx to gordon.
    But i am facing a problem. When i amsubmitting the form it only prompts “Message failed,please send an email to me”.

    It is not emailing the contents to me.

    Jaswant

    • Gordon J. Blair

      Might be that your server does not support PHP mail() function, so the script fails. Though this also can be some server specific configuration, which does not allow execution of the script. In general this should be investigated on the server side

      • Gordon J. Blair

        Another common problem is that some hosts, require all PHP mails to specify a FROM: and TO: address that is in your own domain.
        E.g. if your site is ww w.mydomain.com then the email you use in the form can be only {some-name}@mydomain.com
        A solution to this would be deleting the $headers variable from the mail(…) function

  • http://godscircle.net Patrick

    Tks just looking ill try it when I get home sounds simple tks again

  • http://writer-base.com/story.php?id=707491#comments Robert Moran

    Great, there are actually some great tips on here that my customers might find this relevant, I will email them a link. Thanks

  • Tom

    When I hit the submit button it takes me to a blank page.

  • http://nobull.com billy

    Awesome! great info.

  • Radczyck

    awesome

  • Fizurro

    hi thats cool
    1.when hit send button so where the data will send n how to make the data directly send to my email?
    2.if i want to put some creative out look box then how i want to use that code?

    • Anónimo

      1. To make the messages be setnt to your specific email address edit the $mail_to = ‘test@test-mail.com’; variable. Replace text@text-mail.com with your email address

      2. Unfortunately this can’t be done in this example. To change the window style you need to create the JavaScript based form.

  • Anónimo

    You can try to use this script:

    window.onload=function(){
    var elForm=document.getElementsByTagName(‘form’)[0]; // Get the first form in the document
    elForm.onsubmit=function()
    {
    var required=['First Name','Last Name','Member Name'];
    // Place in this array the name of the form that you think should be mandatory
    var bool=true; // Create bool variable and set its value to true
    for(var i=0;i<required.length;i++)
    {
    if(document.getElementsByName(required[i])[0].value=='')
    {
    alert(required[i]+' textbox is mandatory to fill in.');
    bool=false;
    }
    }
    return bool;
    }
    }

    I recommend you to search google for “HTML form required field”

  • Anónimo

    Add it to the HTML file

  • Lisa

    Hi. I changed only a few things in the php file and saved both locally to my computer. But when I test it out and fill out the form, it gives me a pop up asking if I want to download or save. What am I doing wrong?
    Thanks.
    Lisa

  • Josh

    Hi there,

    Is there anyway to make the message box bigger automatically?

    Thanks,
    Josh

  • Issamht65

    //////////////////////

  • tejinder singh

    thanks alot………..i just downloaded the SINGLE PACKAGE n works on very
    first time