{"id":42,"date":"2024-05-20T09:01:51","date_gmt":"2024-05-20T09:01:51","guid":{"rendered":"https:\/\/fydo.co.uk\/blog\/?p=42"},"modified":"2024-05-20T09:23:01","modified_gmt":"2024-05-20T09:23:01","slug":"how-to-create-a-html-contact-form","status":"publish","type":"post","link":"https:\/\/fydo.co.uk\/blog\/how-to-create-a-html-contact-form\/","title":{"rendered":"How to create a html contact form"},"content":{"rendered":"\n<p>This tutorial will cover the basic elements of a contact form, including input fields for the user&#8217;s name, email, and message, and a submit button. We&#8217;ll also include some basic CSS to style the form.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tutorial: How to Create an HTML Contact Form<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Introduction<\/h4>\n\n\n\n<p>In this tutorial, you will learn how to create a basic HTML contact form that can be embedded on any website. This form will include fields for a user&#8217;s name, email address, and a message. It&#8217;s a great way to allow your site visitors to communicate with you directly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Requirements<\/h4>\n\n\n\n<ul>\n<li>Basic knowledge of HTML and CSS.<\/li>\n\n\n\n<li>A text editor (like VSCode, Sublime Text, or Atom).<\/li>\n\n\n\n<li>A web browser to test the form.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Set Up Your HTML File<\/h4>\n\n\n\n<p>First, create a new HTML file named <code>contact.html<\/code>. You can do this using any text editor.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;!DOCTYPE html&gt;<br>&lt;html lang=\"en\"&gt;<br>&lt;head&gt;<br>    &lt;meta charset=\"UTF-8\"&gt;<br>    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;<br>    &lt;title&gt;Contact Form&lt;\/title&gt;<br>    &lt;link rel=\"stylesheet\" href=\"styles.css\"&gt;<br>&lt;\/head&gt;<br>&lt;body&gt;<br>    &lt;!-- We will add our form here in the next steps --&gt;<br>&lt;\/body&gt;<br>&lt;\/html&gt;<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Create the HTML Form<\/h4>\n\n\n\n<p>Inside the <code>&lt;body&gt;<\/code> tag of your HTML file, add the following HTML to create a form.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;form action=\"submit_form.php\" method=\"POST\"&gt;<br>    &lt;label for=\"name\"&gt;Name:&lt;\/label&gt;<br>    &lt;input type=\"text\" id=\"name\" name=\"name\" required&gt;<br><br>    &lt;label for=\"email\"&gt;Email:&lt;\/label&gt;<br>    &lt;input type=\"email\" id=\"email\" name=\"email\" required&gt;<br><br>    &lt;label for=\"message\"&gt;Message:&lt;\/label&gt;<br>    &lt;textarea id=\"message\" name=\"message\" required&gt;&lt;\/textarea&gt;<br><br>    &lt;button type=\"submit\"&gt;Send&lt;\/button&gt;<br>&lt;\/form&gt;<br><\/code><\/pre>\n\n\n\n<p>Here, the <code>action<\/code> attribute should point to the script that will process the form data. We&#8217;re using <code>submit_form.php<\/code> as a placeholder.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Add Some Styles with CSS<\/h4>\n\n\n\n<p>Create a CSS file named <code>styles.css<\/code> and add the following styles to make your form look better.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>body {<br>    font-family: Arial, sans-serif;<br>    margin: 40px;<br>}<br><br>form {<br>    background-color: #f2f2f2;<br>    padding: 20px;<br>    border-radius: 5px;<br>}<br><br>label {<br>    margin-top: 10px;<br>    display: block;<br>}<br><br>input[type=\"text\"],<br>input[type=\"email\"],<br>textarea {<br>    width: 100%;<br>    padding: 8px;<br>    margin-top: 5px;<br>    margin-bottom: 20px;<br>    border-radius: 4px;<br>    border: 1px solid #ccc;<br>}<br><br>button {<br>    background-color: #4CAF50;<br>    color: white;<br>    border: none;<br>    padding: 10px 20px;<br>    text-align: center;<br>    text-decoration: none;<br>    display: inline-block;<br>    font-size: 16px;<br>    margin: 4px 2px;<br>    cursor: pointer;<br>    border-radius: 4px;<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bonus-free-html5-contact-form-generator\">Bonus: Free HTML5 Contact Form Generator<\/h3>\n\n\n\n<p>We have created a form generator that you can use to build your own contact forms and copy\/paste into your own website with ease. They are free to download and can be used on your personal or commercial projects. Click here to check out our&nbsp;<a href=\"https:\/\/fydo.co.uk\/contact-widget.html\">free contact form generator \u2197\ufe0f<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Create the PHP File<\/h4>\n\n\n\n<p>PHP script that can be used to handle the form data submitted from the HTML contact form we created. This script will process the form inputs and could be used to send an email or save the information to a database. For now, we&#8217;ll keep it simple and just display the submitted data.<\/p>\n\n\n\n<p>Create a file named <code>submit_form.php<\/code> in the same directory as your HTML file. This PHP script will handle the form submission.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&lt;?php<br>\/\/ Check if the form is submitted<br>if ($_SERVER[\"REQUEST_METHOD\"] == \"POST\") {<br>    \/\/ Retrieve the form data<br>    $name = htmlspecialchars($_POST['name']);<br>    $email = htmlspecialchars($_POST['email']);<br>    $message = htmlspecialchars($_POST['message']);<br><br>    \/\/ Simple validation<br>    if (!empty($email) &amp;&amp; filter_var($email, FILTER_VALIDATE_EMAIL)) {<br>        \/\/ Process your form (e.g., send an email, save to a database, etc.)<br>        \/\/ For now, we'll just display the data.<br>        echo \"&lt;h1&gt;Thank you for your message, $name!&lt;\/h1&gt;\";<br>        echo \"&lt;p&gt;We will get back to you at &lt;strong&gt;$email&lt;\/strong&gt;.&lt;\/p&gt;\";<br>        echo \"&lt;p&gt;Your message: $message&lt;\/p&gt;\";<br>    } else {<br>        echo \"Please enter a valid email address.\";<br>    }<br>} else {<br>    \/\/ Not a POST request, redirect to the form<br>    header('Location: contact.html');<br>    exit;<br>}<br>?&gt;<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Explanation of the PHP Script<\/h4>\n\n\n\n<ol>\n<li><strong>Check Form Submission:<\/strong> The script first checks if the form has been submitted using a POST request. This is important to avoid processing data that hasn&#8217;t been sent through the form.<\/li>\n\n\n\n<li><strong>Retrieve and Sanitize Data:<\/strong> It retrieves data from the form using the <code>$_POST<\/code> superglobal. The <code>htmlspecialchars()<\/code> function is used to prevent XSS (Cross-site Scripting) attacks by converting special HTML characters to their HTML entities.<\/li>\n\n\n\n<li><strong>Validation:<\/strong> The script validates the email address to ensure it&#8217;s in a proper format using <code>filter_var()<\/code> with <code>FILTER_VALIDATE_EMAIL<\/code>.<\/li>\n\n\n\n<li><strong>Processing and Response:<\/strong> For the purpose of this tutorial, the script simply displays a thank you message along with the submitted data. In a real application, this could be where you send an email, log a ticket, or save the data to a database.<\/li>\n\n\n\n<li><strong>Error Handling:<\/strong> If the email isn&#8217;t valid, it displays an error message. If the page is accessed without submitting the form, it redirects the user back to the <code>contact.html<\/code> page.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Next Steps<\/h4>\n\n\n\n<ul>\n<li>To actually send an email, you can use the <code>mail()<\/code> function in PHP. You&#8217;ll need to configure your server to send mail for this function to work.<\/li>\n\n\n\n<li>Implement further security measures like using a library for better data sanitization and validation.<\/li>\n\n\n\n<li>To prevent form resubmission when refreshing the page, you could redirect to a new page or implement a token-based system to handle form submissions.<\/li>\n<\/ul>\n\n\n\n<p>This should give you a good foundation for handling contact form data with PHP!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Testing<\/h3>\n\n\n\n<p>Open your <code>contact.html<\/code> file in a web browser to see your form in action. Fill out the fields and press the &#8220;Send&#8221; button to ensure everything is set up correctly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>Congratulations! You&#8217;ve successfully created a basic HTML contact form with styling. This form can be further customized and integrated into your website as needed.<\/p>\n\n\n\n<p>Feel free to add more fields or include advanced features like validation to enhance your form&#8217;s functionality.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will cover the basic elements of a contact form, including input fields for the user&#8217;s name, email, and message, and a submit button. We&#8217;ll also include some basic CSS to style the form. Tutorial: How to Create an HTML Contact Form Introduction In this tutorial, you will learn how to create a basic &#8230; <a title=\"How to create a html contact form\" class=\"read-more\" href=\"https:\/\/fydo.co.uk\/blog\/how-to-create-a-html-contact-form\/\" aria-label=\"Read more about How to create a html contact form\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":45,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[5,16,12,42,43,4,22,20],"_links":{"self":[{"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/posts\/42"}],"collection":[{"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":2,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions\/44"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/media\/45"}],"wp:attachment":[{"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fydo.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}