{"id":8011,"date":"2011-04-20T07:00:26","date_gmt":"2011-04-20T11:00:26","guid":{"rendered":"http:\/\/info.template-help.com\/?p=8011"},"modified":"2013-04-15T07:41:44","modified_gmt":"2013-04-15T11:41:44","slug":"adding-fields-to-the-html-contact-form","status":"publish","type":"post","link":"https:\/\/www.templatemonster.com\/help\/adding-fields-to-the-html-contact-form.html","title":{"rendered":"Adding fields to the HTML contact form"},"content":{"rendered":"<p>This tutorial will show you how to <strong>add fields to the HTML contact form<\/strong>. This tutorial is a continuation of the tutorial <a href=\"\/help\/how-create-contact-form-html.html\">How to create a contact form in HTML<\/a>. If you have the files from that tutorial then open to continue on, or else <a href=\"http:\/\/info.template-help.com\/\/files\/Html\/html-contact-form.zip\">download them<\/a><\/p>\r\n\r\n<h3>Part 1. HTML<\/h3>\r\n\r\n<p>Open the <strong>index.html<\/strong> file with your prefered HTML editor. We have 2 input, 1 textarea element and two buttons. So let&#8217;s add more features to it, like checkboxes, radio buttons and a drop-down list. The HTML syntax for all those elements is quite easy and you can check it below. Add this code right after the Submit and Reset buttons.<\/p>\r\n\r\n<p>Additional options:<\/p>\r\n<pre class=\"brush:html\">\r\n\t&lt;input type=\"checkbox\" name=\"check[]\" value=\"USA\"&gt;USA&lt;br \/&gt;\r\n\t&lt;input type=\"checkbox\" name=\"check[]\" value=\"Canada\"&gt;Canada&lt;br \/&gt;\r\n\t&lt;input type=\"checkbox\" name=\"check[]\" value=\"Mexico\"&gt;Mexico&lt;br \/&gt;\r\n<\/pre>\r\n\r\n<p>Do you agree?<\/p>\r\n<pre class=\"brush:html\">\r\n&lt;input type=\"radio\" value=\"yes\" name=\"cf_radio_button\"&gt;Yes\r\n&lt;input type=\"radio\" value=\"no\" name=\"cf_radio_button\"&gt;No\r\n<\/pre>\r\n\r\n<p>Select an item from the drop-down:<\/p>\r\n<pre class=\"brush:html\">\r\n&lt;select size=\"1\" name=\"cf_drop_down\"&gt;\r\n\t&lt;option&gt;php&lt;\/option&gt;\r\n\t&lt;option&gt;html&lt;\/option&gt;\r\n\t&lt;option&gt;css&lt;\/option&gt;\r\n\t&lt;option&gt;asp&lt;\/option&gt;\r\n\t&lt;option&gt;ajax&lt;\/option&gt;\r\n\t&lt;option&gt;javascript&lt;\/option&gt;\r\n&lt;\/select&gt;\r\n<\/pre>\r\n\r\n<p>Let&#8217;s clarify some interesting moments in this code. You might have noticed that the names assigned to the checkboxes are not different, as in case with text field added previously, also they have square brackets at the end. The brackets define that this is an array variable.\r\nSo this is how it works. For each checkbox you check, the <strong>check[]<\/strong> array receives the value of the checkboxes pressed. For example, after checking both the USA and Canada checkboxes, your <strong>check[]<\/strong> array contains the values <strong>checked_usa<\/strong> and <strong>check_canada<\/strong> as its elements. <\/p>\r\n\r\n<p>Nothing fancy going on with the radion buttons or drop-down. We&#8217;ve assigned the same name &#8220;<strong>cf_radio_button<\/strong>&#8221; to both of the radio buttons, as you can&#8217;t select both at the same time, so each of them don&#8217;t need to have a unique name. <\/p>\r\n<p>The drop-down list syntax is quite easy as well. Open the tag <strong>&lt;select&gt;<\/strong>, then list inside it the options, wrapped in <strong>&lt;option&gt;&#8230;&lt;\/option&gt;<\/strong> tags, and at the end close with a <strong>&lt;\/select&gt;<\/strong> tag. The &lt;select&gt; tag has 2 attributes assigned to it, one of which is name, and the second &#8211; size. The size attribute specifies the number of visible options in a drop-down list.<strong>&lt;\/select&gt;<\/strong><\/p>\r\n\r\n<p>So here&#8217;s the result you should get with that code added.<\/p>\r\n\r\n<p align=\"center\">\r\n<a class=\"darkbox\" href=\"http:\/\/info.template-help.com\/wp-content\/uploads\/2011\/04\/contact_form_add_lements.jpg\"><img src=\"http:\/\/info.template-help.com\/wp-content\/uploads\/2011\/04\/contact_form_add_lements-248x300.jpg\" alt=\"HTML contact form\" width=\"248\" class=\"alignnone size-medium wp-image-8013\" srcset=\"https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2011\/04\/contact_form_add_lements-248x300.jpg 248w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2011\/04\/contact_form_add_lements-124x150.jpg 124w, https:\/\/www.templatemonster.com\/help\/wp-content\/uploads\/2011\/04\/contact_form_add_lements.jpg 312w\" sizes=\"(max-width: 248px) 100vw, 248px\" \/><span class=\"enlarge\">(click to enlarge)<\/span><\/a>\r\n<\/p>\r\n\r\n<h3>Part 2. PHP<\/h3>\r\n\r\n<p>So, now that we have the elements on the page, we should proccess their data sent and add to the email sent. Below, is the final code of <strong>contact.php<\/strong> file<\/p>\r\n\r\n<pre class=\"brush:php\">\r\n&lt;?php\r\n\/\/ Grabbing data sent from the form and assigning it to variables\r\n$field_name = $_POST['cf_name'];\r\n$field_email = $_POST['cf_email'];\r\n$field_message = $_POST['cf_message'];\r\n\r\nforeach($_POST['check'] as $value) {\r\n\t$check_boxes .= $value.\" \";\r\n}\r\n\r\n$radio_button = $_POST['cf_radio_button'];\r\n$drop_down_item = $_POST['cf_drop_down'];\r\n\r\n\/\/ Composing body of the message\r\n$mail_to = 'gordon@template-help.com';\r\n$subject = 'Message from a site visitor '.$field_name;\r\n\r\n$body_message = 'From: '.$field_name.\"\\n\";\r\n$body_message .= 'E-mail: '.$field_email.\"\\n\";\r\n$body_message .= 'Message: '.$field_message.\"\\n\";\r\n$body_message .= \"Additional options checked: \".$check_boxes.\"\\n\";\r\n$body_message .= \"Did the customer agree: \".$radio_button.\"\\n\";\r\n$body_message .= \"Selected item from the dropdown list: \".$drop_down_item;\r\n\r\n\/\/ Creating headers of the message\r\n$headers = 'From: '.$field_email.\"\\r\\n\";\r\n$headers .= 'Reply-To: '.$field_email.\"\\r\\n\";\r\n\r\n$mail_status = mail($mail_to, $subject, $body_message, $headers);\r\n\r\nif ($mail_status) { ?&gt;\r\n\t&lt;script language=\"javascript\" type=\"text\/javascript\"&gt;\r\n\t\talert('Thank you for the message. We will contact you shortly.');\r\n\t\thistory.back(1);\r\n\t&lt;\/script&gt;\r\n&lt;?php\r\n}\r\n\r\nelse { ?&gt;\r\n\t&lt;script language=\"javascript\" type=\"text\/javascript\"&gt;\r\n\t\talert('Message failed. Please, send an email to gordon@template-help.com');\r\n\t\thistory.back(1);\r\n\t&lt;\/script&gt;\r\n&lt;?php\r\n}\r\n?&gt;\r\n<\/pre>\r\n\r\n<p>Let&#8217;s see what parts of code have been added here, and what they actually do. <\/p>\r\n\r\n<p>We&#8217;ll use the php function <strong>foreach()<\/strong> to iterate through an array &#8220;check&#8221; and store each element of the array into a variable called <strong>$value<\/strong>. Then we create a variable <strong>$check_boxes<\/strong> and increment it with the value of each element in your array<\/p>\r\n\r\n<pre class=\"brush:php\">\r\nforeach($_POST['check'] as $value) {\r\n\t$check_boxes .= $value.\" \";\r\n}\r\n<\/pre>\r\n\r\n<p>As you see below, the radio buttons and drop-down list are defined in a similar way to input text fields. We create a php variable and assign it the data sent from a radio box or the drop-down list<\/p>\r\n\r\n<pre class=\"brush:php\">\r\n$radio_button = $_POST['cf_radio_button'];\r\n$drop_down_item = $_POST['cf_drop_down'];\r\n<\/pre>\r\n\r\n<p>This piece of code is also similar to what we&#8217;ve done before. We just add new data to the final email<\/p>\r\n\r\n<pre class=\"brush:php\">\r\n$body_message .= \"Additional options checked: \".$check_boxes.\"\\n\";\r\n$body_message .= \"Did the customer agree: \".$radio_button.\"\\n\";\r\n$body_message .= \"Selected item from the dropdown list: \".$drop_down_item;\r\n<\/pre>\r\n\r\n<p>\r\n<strong>TIP:<\/strong> One small update that might be usefull. In the previous version of the script, after the javascript alert message, there was a manual redirect to the page you specify in the code <strong>window.location = &#8216;contact_page.html&#8217;;<\/strong> So if you rename the <strong>contact_page.html<\/strong> to something else, you&#8217;ll need to correct the file name in <strong>contact.php<\/strong> A better way would be replacing that code with this one <\/p>\r\n\r\n<pre class=\"brush:php\">\r\nhistory.back(1);\r\n<\/pre>\r\n\r\n<p>Basically the code itself explains what it does. This is the same as clicking the <strong>Back<\/strong> button in your browser.<\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial will show you how to add fields to the HTML contact form. This tutorial is a continuation of the tutorial How to create a contact form in HTML. If you have the files from that tutorial then open to continue on, or else download them Part 1. HTML Open the index.html file with [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[253],"tags":[639,907,640,2411,910,911,909,908],"_links":{"self":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/8011"}],"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=8011"}],"version-history":[{"count":0,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/posts\/8011\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/media?parent=8011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/categories?post=8011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.templatemonster.com\/help\/wp-json\/wp\/v2\/tags?post=8011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}