{"id":1709,"date":"2011-11-15T10:24:58","date_gmt":"2011-11-15T15:24:58","guid":{"rendered":"http:\/\/mts-help.nick.php.dev\/after_sale_support\/working_with_html\/Advanced\/article14.html"},"modified":"2016-11-11T04:09:32","modified_gmt":"2016-11-11T09:09:32","slug":"how-create-contact-form-html","status":"publish","type":"post","link":"https:\/\/www.templatemonster.com\/help\/how-create-contact-form-html.html","title":{"rendered":"How to create a contact form in HTML"},"content":{"rendered":"<p>This tutorial will teach you how to create a very simple contact form for HTML based website template.<\/p>\r\n\r\n<p>First of all create 2 files: <strong>contact_form.html<\/strong> and <strong>contact.php<\/strong>. The first file will contain HTML code for the form and the second -will process the data from the form<\/p>\r\n<br\/>\r\n<h3>HTML<\/h3>\r\n\r\n<p>Below is the HTML code of the contact form<\/p>\r\n\r\n<pre class=\"brush:html\">\r\n&lt;form action=\"contact.php\" method=\"post\">\r\n\tYour name<br>\r\n    &lt;input type=\"text\" name=\"cf_name\"><br>\r\n\tYour e-mail<br>\r\n    &lt;input type=\"text\" name=\"cf_email\"><br>\r\n\tMessage<br>\r\n    &lt;textarea name=\"cf_message\"><\/textarea><br>\r\n\t&lt;input type=\"submit\" value=\"Send\">\r\n\t&lt;input type=\"reset\" value=\"Clear\">\r\n&lt;\/form>\r\n<\/pre>\r\n\r\n<p>And this is how it will look in the browser<\/p>\r\n<a href=\"http:\/\/info.template-help.com\/wp-content\/uploads\/2010\/11\/html-contact-form-layout.jpg\"><img loading=\"lazy\" src=\"http:\/\/info.template-help.com\/wp-content\/uploads\/2010\/11\/html-contact-form-layout.jpg\" alt=\"HTML contact form\" title=\"html-contact-form-layout\" width=\"202\" height=\"188\" class=\"alignleft size-full wp-image-7726\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2010\/11\/html-contact-form-layout.jpg 202w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2010\/11\/html-contact-form-layout-150x140.jpg 150w\" sizes=\"(max-width: 202px) 100vw, 202px\" \/><\/a>\r\n\r\n<p>Let&#8217;s have a quick look at some main aspects of it. The &lt;form> tag should have 2 additional attributes: <\/p>\r\n<p><strong>action=&#8221;contact.php&#8221;<\/strong> &#8211; this attribute specifies where to send the data from the contact form fields, when it has been submitted<\/p>\r\n<strong>method=&#8221;post&#8221;<\/strong> &#8211; this attribute specifies how to send data from the form to the file specified in the <strong>action<\/strong> attribute<\/p>\r\n\r\nThe &lt;input> and &lt;textarea> tags should have an attribute &#8220;name&#8221; with a unique identifier. This attribute is used to identify form data after it has been submitted to the server\r\nAnd the 2 input elements that are used as Submit and Clear buttons, one should have type=&#8221;submit&#8221; assigned to it and the other type=&#8221;reset&#8221;<\/p>\r\n\r\n<p>That&#8217;s basically it. As easy as it looks<\/p>\r\n\r\n<h3>PHP<\/h3>\r\n\r\n<p>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 <a href=\"files\/Html\/contact-php.zip\">contact.php<\/a> file from this link. Below is the code of the file with comments to its major sections.<\/p>\r\n\r\nAssigning the data sent from the contact form fields (cf_name, cf_email, cf_message) to php variables ($cf_message, $field_email, $field_message)\r\n<pre class=\"brush:php\">\r\n$field_name = $_POST['cf_name'];\r\n$field_email = $_POST['cf_email'];\r\n$field_message = $_POST['cf_message'];\r\n<\/pre>\r\n\r\n<p><strong>$mail_to<\/strong> 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)<\/p>\r\n<pre class=\"brush:php\">\r\n$mail_to = 'test&#64;test-mail.com';\r\n<\/pre>\r\n\r\n<p>Subject of the email you receive from the contact form<\/p>\r\n<pre class=\"brush:php\">\r\n$subject = 'Message from a site visitor ' . $field_name;\r\n<\/pre>\r\n\r\n<p>Constructing the body of the message<\/p>\r\n<pre class=\"brush:php\">\r\n$body_message = 'From: '.$field_name.\"\\n\";\r\n$body_message .= 'E-mail: '.$field_email.\"\\n\";\r\n$body_message .= 'Message: '.$field_message;\r\n<\/pre>\r\n\r\n<p>Constructing the headers of the message<\/p>\r\n<pre class=\"brush:php\">\r\n$headers = \"From: $cf_email\\r\\n\";\r\n$headers .= \"Reply-To: $cf_email\\r\\n\";\r\n<\/pre>\r\n\r\n<p>Defining <strong>mail() <\/strong>function and assigning it to a variable $mail_status, which is used below to check whether the mail has been sent or not<\/p>\r\n<pre class=\"brush:php\">\r\n$mail_status = mail($mail_to, $subject, $body_message, $headers);\r\n<\/pre>\r\n\r\n<p>If the<strong> mail()<\/strong> function executed successfully then do the code below\r\n<\/p>\r\n<pre class=\"brush:js\">\r\nif ($mail_status) { ?&gt;\r\n\t&lt;script language=\"javascript\" type=\"text\/javascript\">\r\n\t\t\/\/ Print a message\r\n\t\talert('Thank you for the message. We will contact you shortly.');\r\n\t\t\/\/ Redirect to some page of the site. You can also specify full URL, e.g. http:\/\/template-help.com\r\n\t\twindow.location = 'contact_page.html';\r\n\t&lt;\/script&gt;\r\n&lt;?php\r\n}\r\n<\/pre>\r\n\r\n<p>If the <strong>mail()<\/strong> function fails, then execute the following code<\/p>\r\n<pre class=\"brush:js\">\r\nelse { ?&gt;\r\n\t&lt;script language=\"javascript\" type=\"text\/javascript\"&gt;\r\n\t\t\/\/ Print a message\r\n\t\talert('Message failed. Please, send an email to gordon&#64;template-help.com');\r\n\t\t\/\/ Redirect to some page of the site. You can also specify full URL, e.g. http:\/\/template-help.com\r\n\t\twindow.location = 'contact_page.html';\r\n\t&lt;\/script&gt;\r\n&lt;?php\r\n}?&gt;\r\n<\/pre>\r\n<p>\r\nYou can also download compiled contact_form.html and contact.php files in a <a href=\"http:\/\/info.template-help.com\/\/files\/Html\/html-contact-form.zip\">single package<\/a><\/p>\r\n\r\n<p>&nbsp;<\/p>\r\n<p>Proceed to the tutorial on<a href=\"\/help\/adding-fields-to-the-html-contact-form.html\"> how to add fields to the contact form<\/a><\/p>\r\n<p>If you want to pep up the design of your admin panel, review our <a href=\"https:\/\/www.templatemonster.com\/admin-templates.php\" title=\"Bootstrap Admin Themes\" target=\"_blank\">Bootstrap Admin Themes<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial will teach you how to create a very simple contact form for HTML based website template.<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[253],"tags":[497,509,2411],"_links":{"self":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/1709"}],"collection":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/comments?post=1709"}],"version-history":[{"count":0,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/1709\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/media?parent=1709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/categories?post=1709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/tags?post=1709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}