{"id":2532,"date":"2013-03-27T14:19:13","date_gmt":"2013-03-27T14:19:13","guid":{"rendered":"https:\/\/www.inmotionhosting.com\/support\/2013\/03\/27\/how-to-create-a-custom-php-contact-form\/"},"modified":"2023-08-15T18:15:45","modified_gmt":"2023-08-15T22:15:45","slug":"how-to-create-a-custom-php-contact-form","status":"publish","type":"post","link":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/","title":{"rendered":"How to Create a Custom PHP Contact Form with Validation"},"content":{"rendered":"<p><a href=\"https:\/\/www.inmotionhosting.com\/php-hosting\"><strong>PHP<\/strong><\/a> is a great scripting language that allows many dynamic functions in your site. You can create custom <strong>contact<\/strong> forms, <strong>form<\/strong> validation, and email responses using PHP. This article will explain the basics in creating an email form that validates the inputs, produces errors when inputs are typed incorrectly, and send an email to you when submitted.<\/p>\n<h2>This section of the code will validate the form inputs<\/h2>\n<p>Below is the code you will use to validate whether the inputs have valid data or not. This can be customized for different form field validations.<\/p>\n<p class=\"alert alert-info\"><strong>Note!<\/strong> You can paste the entire code directly in the body section of your webpage to get it working.<\/p>\n<pre style=\"padding: 10px;\">&lt;?php \nif (isset($_REQUEST['submitted'])) {\n\/\/ Initialize error array.\n  $errors = array();\n  \/\/ Check for a proper First name\n  if (!empty($_REQUEST['firstname'])) {\n  $firstname = $_REQUEST['firstname'];\n  $pattern = \"\/^[a-zA-Z0-9\\_]{2,20}\/\";\/\/ This is a regular expression that checks if the name is valid characters\n  if (preg_match($pattern,$firstname)){ $firstname = $_REQUEST['firstname'];}\n  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}\n  } else {$errors[] = 'You forgot to enter your First Name.';}\n  \n  \/\/ Check for a proper Last name\n  if (!empty($_REQUEST['lastname'])) {\n  $lastname = $_REQUEST['lastname'];\n  $pattern = \"\/^[a-zA-Z0-9\\_]{2,20}\/\";\/\/ This is a regular expression that checks if the name is valid characters\n  if (preg_match($pattern,$lastname)){ $lastname = $_REQUEST['lastname'];}\n  else{ $errors[] = 'Your Name can only contain _, 1-9, A-Z or a-z 2-20 long.';}\n  } else {$errors[] = 'You forgot to enter your Last Name.';}\n  \n  \/\/Check for a valid phone number\n  if (!empty($_REQUEST['phone'])) {\n  $phone = $_REQUEST['phone'];\n  $pattern = \"\/^[0-9\\_]{7,20}\/\";\n  if (preg_match($pattern,$phone)){ $phone = $_REQUEST['phone'];}\n  else{ $errors[] = 'Your Phone number can only be numbers.';}\n  } else {$errors[] = 'You forgot to enter your Phone number.';}\n  \n  if (!empty($_REQUEST['redmapleacer']) || !empty($_REQUEST['chinesepistache']) || !empty($_REQUEST['raywoodash'])) {\n  $check1 = $_REQUEST['redmapleacer'];\n  if (empty($check1)){$check1 = 'Unchecked';}else{$check1 = 'Checked';}\n  $check2 = $_REQUEST['chinesepistache'];\n  if (empty($check2)){$check2 = 'Unchecked';}else{$check2 = 'Checked';}\n  $check3 = $_REQUEST['raywoodash'];\n  if (empty($check3)){$check3 = 'Unchecked';}else{$check3 = 'Checked';}\n  } else {$errors[] = 'You forgot to enter your Phone number.';}\n  }\n  \/\/End of validation<\/pre>\n<h2>Sends the email if validation passes<\/h2>\n<p>The following code is what sends the email. The inputs must pass the previous validation in order for the email to send. You will need to replace the &#8220;<em>to<\/em>&#8221; email address with the email address you want to receive the email to.<\/p>\n<pre style=\"padding: 10px;\">if (isset($_REQUEST['submitted'])) {\n  if (empty($errors)) { \n  $from = \"From: Our Site!\"; \/\/Site name\n  \/\/ Change this to your email address you want to form sent to\n  $to = \"your@email.com\"; \n  $subject = \"Admin - Our Site! Comment from \" . $name . \"\";\n  \n  $message = \"Message from \" . $firstname . \" \" . $lastname . \" \n  Phone: \" . $phone . \" \n  Red Maple Acer: \" . $check1 .\"\n  Chinese Pistache: \" . $check2 .\"\n  Raywood Ash: \" . $check3 .\"\";\n  mail($to,$subject,$message,$from);\n  }\n}\n?&gt;<\/pre>\n<h2>Error Reporting Code<\/h2>\n<pre style=\"padding: 10px;\">&lt;?php \n  \/\/Print Errors\n  if (isset($_REQUEST['submitted'])) {\n  \/\/ Print any error messages. \n  if (!empty($errors)) { \n  echo '&lt;hr \/&gt;&lt;h3&gt;The following occurred:&lt;\/h3&gt;&lt;ul&gt;'; \n  \/\/ Print each error. \n  foreach ($errors as $msg) { echo '&lt;li&gt;'. $msg . '&lt;\/li&gt;';}\n  echo '&lt;\/ul&gt;&lt;h3&gt;Your mail could not be sent due to input errors.&lt;\/h3&gt;&lt;hr \/&gt;';}\n   else{echo '&lt;hr \/&gt;&lt;h3 align=\"center\"&gt;Your mail was sent. Thank you!&lt;\/h3&gt;&lt;hr \/&gt;&lt;p&gt;Below is the message that you sent.&lt;\/p&gt;'; \n  echo \"Message from \" . $firstname . \" \" . $lastname . \" \nPhone: \".$phone.\" \n\";\n  echo \"\nRed Maple Acer: \" . $check3 . \"\";\n  echo \"\nChinese Pistache: \" . $check2 . \"\";\n  echo \"\nRaywood Ash: \" . $check3 . \"\";\n  }\n  }\n\/\/End of errors array\n  ?&gt;<\/pre>\n<h2>Prints the contact form<\/h2>\n<p>This is the form that will display for the visitor to fill out.<\/p>\n<h2>Contact us<\/h2>\n<p>&nbsp;<\/p>\n<p>Fill out the form below.<\/p>\n<p>&nbsp;<\/p>\n<form action=\"\" method=\"post\">\n<label>First Name:<\/p>\n<p><input name=\"firstname\" type=\"text\" value=\"- Enter First Name -\" \/><br \/>\n<\/label><br \/>\n<label>Last Name:<\/p>\n<p><input name=\"lastname\" type=\"text\" value=\"- Enter Last Name -\" \/><br \/>\n<\/label><br \/>\n<label>Phone Number:<\/p>\n<p><input name=\"phone\" type=\"text\" value=\"- Enter Phone Number -\" \/><br \/>\n<\/label><br \/>\n<label>Red Maple Acer:<br \/>\n<input name=\"redmapleacer\" type=\"checkbox\" value=\"Red Maple Acer\" \/><br \/>\n<\/label><br \/>\n<label>Chinese Pistache:<br \/>\n<input name=\"chinesepistache\" type=\"checkbox\" value=\"Chinese Pistache\" \/><br \/>\n<\/label><br \/>\n<label>Raywood Ash:<br \/>\n<input name=\"raywoodash\" type=\"checkbox\" value=\"Raywood Ash\" \/><br \/>\n<\/label><br \/>\n<input name=\"\" type=\"reset\" value=\"Reset Form\" \/><input name=\"submitted\" type=\"submit\" value=\"Submit\" \/><\/form>\n<pre style=\"padding: 10px;\"><\/pre>\n<p>You can paste the entire code directly in the body section of your webpage to get it working. We have more tutorials with other methods to send email from your website at the following links.<\/p>\n<p><a href=\"\/support\/email\/using-phpmailer-to-send-mail-through-php\/\" target=\"_blank\" rel=\"noopener\">Using phpMailer to Send Mail through PHP<\/a><\/p>\n<p><a href=\"\/support\/email\/how-to-set-up-formmail\/\" target=\"_blank\" rel=\"noopener\">How to set up FormMail <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP is a great scripting language that allows many dynamic functions in your site. You can create custom contact forms, form validation, and email responses using PHP. This article will explain the basics in creating an email form that validates the inputs, produces errors when inputs are typed incorrectly, and send an email to you<a class=\"moretag\" href=\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\"> Read More ><\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4288],"tags":[],"class_list":["post-2532","post","type-post","status-publish","format-standard","hentry","category-website"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create a Custom PHP Contact Form with Validation | InMotion Hosting<\/title>\n<meta name=\"description\" content=\"Looking for a custom PHP mail form? We have one that you can just copy and paste from within this article.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create a Custom PHP Contact Form with Validation | InMotion Hosting\" \/>\n<meta property=\"og:description\" content=\"Looking for a custom PHP mail form? We have one that you can just copy and paste from within this article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\" \/>\n<meta property=\"og:site_name\" content=\"InMotion Hosting Support Center\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/inmotionhosting\/\" \/>\n<meta property=\"article:published_time\" content=\"2013-03-27T14:19:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-15T22:15:45+00:00\" \/>\n<meta name=\"author\" content=\"InMotion Hosting Contributor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:site\" content=\"@InMotionHosting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InMotion Hosting Contributor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\"},\"author\":{\"name\":\"InMotion Hosting Contributor\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0ba9f3ef6fadef71df5aa120ff996c41\"},\"headline\":\"How to Create a Custom PHP Contact Form with Validation\",\"datePublished\":\"2013-03-27T14:19:13+00:00\",\"dateModified\":\"2023-08-15T22:15:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\"},\"wordCount\":259,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"articleSection\":[\"Website\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\",\"name\":\"How to Create a Custom PHP Contact Form with Validation | InMotion Hosting\",\"isPartOf\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\"},\"datePublished\":\"2013-03-27T14:19:13+00:00\",\"dateModified\":\"2023-08-15T22:15:45+00:00\",\"description\":\"Looking for a custom PHP mail form? We have one that you can just copy and paste from within this article.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.inmotionhosting.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create a Custom PHP Contact Form with Validation\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#website\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"name\":\"InMotion Hosting Support Center\",\"description\":\"Web Hosting Support &amp; Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#organization\",\"name\":\"InMotion Hosting\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"contentUrl\":\"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg\",\"width\":696,\"height\":696,\"caption\":\"InMotion Hosting\"},\"image\":{\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/inmotionhosting\/\",\"https:\/\/x.com\/InMotionHosting\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0ba9f3ef6fadef71df5aa120ff996c41\",\"name\":\"InMotion Hosting Contributor\",\"url\":\"https:\/\/www.inmotionhosting.com\/support\/author\/jamesr\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create a Custom PHP Contact Form with Validation | InMotion Hosting","description":"Looking for a custom PHP mail form? We have one that you can just copy and paste from within this article.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/","og_locale":"en_US","og_type":"article","og_title":"How to Create a Custom PHP Contact Form with Validation | InMotion Hosting","og_description":"Looking for a custom PHP mail form? We have one that you can just copy and paste from within this article.","og_url":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/","og_site_name":"InMotion Hosting Support Center","article_publisher":"https:\/\/www.facebook.com\/inmotionhosting\/","article_published_time":"2013-03-27T14:19:13+00:00","article_modified_time":"2023-08-15T22:15:45+00:00","author":"InMotion Hosting Contributor","twitter_card":"summary_large_image","twitter_creator":"@InMotionHosting","twitter_site":"@InMotionHosting","twitter_misc":{"Written by":"InMotion Hosting Contributor","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#article","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/"},"author":{"name":"InMotion Hosting Contributor","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0ba9f3ef6fadef71df5aa120ff996c41"},"headline":"How to Create a Custom PHP Contact Form with Validation","datePublished":"2013-03-27T14:19:13+00:00","dateModified":"2023-08-15T22:15:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/"},"wordCount":259,"commentCount":0,"publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"articleSection":["Website"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/","url":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/","name":"How to Create a Custom PHP Contact Form with Validation | InMotion Hosting","isPartOf":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#website"},"datePublished":"2013-03-27T14:19:13+00:00","dateModified":"2023-08-15T22:15:45+00:00","description":"Looking for a custom PHP mail form? We have one that you can just copy and paste from within this article.","breadcrumb":{"@id":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.inmotionhosting.com\/support\/website\/how-to-create-a-custom-php-contact-form\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.inmotionhosting.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Create a Custom PHP Contact Form with Validation"}]},{"@type":"WebSite","@id":"https:\/\/www.inmotionhosting.com\/support\/#website","url":"https:\/\/www.inmotionhosting.com\/support\/","name":"InMotion Hosting Support Center","description":"Web Hosting Support &amp; Tutorials","publisher":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.inmotionhosting.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.inmotionhosting.com\/support\/#organization","name":"InMotion Hosting","url":"https:\/\/www.inmotionhosting.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","contentUrl":"https:\/\/www.inmotionhosting.com\/support\/wp-content\/uploads\/2023\/02\/inmotion-hosting-logo-yoast.jpg","width":696,"height":696,"caption":"InMotion Hosting"},"image":{"@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/inmotionhosting\/","https:\/\/x.com\/InMotionHosting"]},{"@type":"Person","@id":"https:\/\/www.inmotionhosting.com\/support\/#\/schema\/person\/0ba9f3ef6fadef71df5aa120ff996c41","name":"InMotion Hosting Contributor","url":"https:\/\/www.inmotionhosting.com\/support\/author\/jamesr\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"primary_category":null,"_links":{"self":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/2532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/comments?post=2532"}],"version-history":[{"count":2,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/2532\/revisions"}],"predecessor-version":[{"id":106045,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/posts\/2532\/revisions\/106045"}],"wp:attachment":[{"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/media?parent=2532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/categories?post=2532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inmotionhosting.com\/support\/wp-json\/wp\/v2\/tags?post=2532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}