{"id":979,"date":"2021-04-09T04:45:23","date_gmt":"2021-04-09T04:45:23","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=979"},"modified":"2025-04-07T09:33:23","modified_gmt":"2025-04-07T09:33:23","slug":"php-form-validation","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-form-validation\/","title":{"rendered":"PHP Form Validation"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about PHP form validation, how to validate form data, and how to show error messages if the user inputs are invalid.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-php-form-validation'>Introduction to PHP form validation <a href=\"#introduction-to-php-form-validation\" class=\"anchor\" id=\"introduction-to-php-form-validation\" title=\"Anchor for Introduction to PHP form validation\">#<\/a><\/h2>\n\n\n\n<p>When processing a form, it&#8217;s critical to validate user inputs to ensure that the data is in a valid format.<\/p>\n\n\n\n<p>There are two types of validations: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Client-side validation<\/li>\n\n\n\n<li>Server-side validation<\/li>\n<\/ul>\n\n\n\n<p>The client-side validation provides instant feedback to the user, while the server-side validation can ensure that all data is valid before processing such as saving to database. <\/p>\n\n\n\n<p class=\"note\">To validate data at the client side, you can use HTML5 validation or <a href=\"https:\/\/www.javascripttutorial.net\/javascript-dom\/javascript-form\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a>. <\/p>\n\n\n\n<p>The server-side validation validates data in the server using PHP. <\/p>\n\n\n\n<p>To validate data in PHP, you can use filters with the following filter funtions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-filter_has_var\/\">filter_has_var<\/a><\/code> &#8211; check if a variable exists in the <code>GET<\/code> and <code>POST<\/code> requests.<\/li>\n\n\n\n<li><code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-filter_input\/\">filter_input<\/a><\/code> &#8211; validate data.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='validating-emails'>Validating emails <a href=\"#validating-emails\" class=\"anchor\" id=\"validating-emails\" title=\"Anchor for Validating emails\">#<\/a><\/h2>\n\n\n\n<p>The following shows how to check if the email is in the <code>POST<\/code> request and validate it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;form action=<span class=\"hljs-string\">\"&lt;?= htmlspecialchars($_SERVER&#91;'PHP_SELF']) ?&gt;\"<\/span> method=<span class=\"hljs-string\">\"post\"<\/span>&gt;\n    &lt;div&gt;\n        &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"email\"<\/span>&gt;Email:&lt;\/label&gt;\n        &lt;input type=<span class=\"hljs-string\">\"text\"<\/span> name=<span class=\"hljs-string\">\"email\"<\/span>&gt;\n        &lt;button type=<span class=\"hljs-string\">\"submit\"<\/span>&gt;Submit&lt;\/button&gt;\n    &lt;\/div&gt;\n&lt;\/form&gt;\n\n<span class=\"hljs-meta\">&lt;?php<\/span> \n\n<span class=\"hljs-keyword\">if<\/span>($_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>] === <span class=\"hljs-string\">'POST'<\/span>) {\n    <span class=\"hljs-comment\">\/\/ Check if the email field is set and not empty<\/span>\n    <span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>)) {\n        <span class=\"hljs-comment\">\/\/ validate email<\/span>\n        $email = filter_input(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>, FILTER_VALIDATE_EMAIL);\n        <span class=\"hljs-keyword\">if<\/span>($email !== <span class=\"hljs-keyword\">false<\/span>) {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Email is valid: \"<\/span> . htmlspecialchars($email);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Invalid email format.\"<\/span> . $_POST&#91;<span class=\"hljs-string\">'email'<\/span>];\n        }\n    } \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, check if the email is in the <code>POST<\/code> request using the <code>filter_has_var<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>)) {<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, validate email using the <code>filter_input<\/code> function with the filter id <code>FILTER_VALIDATE_EMAIL<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$email = filter_input(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>, FILTER_VALIDATE_EMAIL);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the email is not valid, the <code>filter_input<\/code> function returns <code>false<\/code>. If the email is valid, then the function returns the <code>email<\/code>.<\/p>\n\n\n\n<p>Third, display an error message if the email is not valid or a success message otherwise:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>($email !== <span class=\"hljs-keyword\">false<\/span>) {\n   <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Email is valid: \"<\/span> . htmlspecialchars($email);\n} <span class=\"hljs-keyword\">else<\/span> {\n   <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Invalid email format.\"<\/span> . $_POST&#91;<span class=\"hljs-string\">'email'<\/span>];\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='validating-integers'>Validating integers <a href=\"#validating-integers\" class=\"anchor\" id=\"validating-integers\" title=\"Anchor for Validating integers\">#<\/a><\/h2>\n\n\n\n<p>The following form requests you to enter your age and validate it as an integer with the valid range of (0,150):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n<span class=\"hljs-keyword\">if<\/span>($_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>] === <span class=\"hljs-string\">'POST'<\/span>) {\n    <span class=\"hljs-comment\">\/\/ Check if the age field is set and not empty<\/span>\n    <span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'age'<\/span>)) {\n        <span class=\"hljs-comment\">\/\/ validate age between 0 and 150<\/span>\n        $age = filter_input(INPUT_POST, <span class=\"hljs-string\">'age'<\/span>, FILTER_VALIDATE_INT, &#91;\n            <span class=\"hljs-string\">'options'<\/span> =&gt; &#91;\n                <span class=\"hljs-string\">'min_range'<\/span> =&gt; <span class=\"hljs-number\">0<\/span>,\n                <span class=\"hljs-string\">'max_range'<\/span> =&gt; <span class=\"hljs-number\">150<\/span>\n            ]\n        ]);\n\n        <span class=\"hljs-keyword\">if<\/span>($age !== <span class=\"hljs-keyword\">false<\/span>) {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Age is valid: \"<\/span> . htmlspecialchars($age);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Age is not valid:\"<\/span> . $_POST&#91;<span class=\"hljs-string\">'age'<\/span>];\n        }\n    } \n}\n<span class=\"hljs-meta\">?&gt;<\/span> \n\n&lt;form action=<span class=\"hljs-string\">\"&lt;?= htmlspecialchars($_SERVER&#91;'PHP_SELF']) ?&gt;\"<\/span> method=<span class=\"hljs-string\">\"post\"<\/span>&gt;\n    &lt;div&gt;\n        &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"age\"<\/span>&gt;Age:&lt;\/label&gt;\n        &lt;input type=<span class=\"hljs-string\">\"text\"<\/span> name=<span class=\"hljs-string\">\"age\"<\/span> placeholder=<span class=\"hljs-string\">\"Enter your age\"<\/span>&gt;\n        &lt;button type=<span class=\"hljs-string\">\"submit\"<\/span>&gt;Submit&lt;\/button&gt;\n    &lt;\/div&gt;\n&lt;\/form&gt;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, check if the age is in the <code>POST<\/code> request using the <code>filter_has_var<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'age'<\/span>)) {<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, validate age using the <code>filter_input<\/code> function with the filter id <code>FILTER_VALIDATE_INT<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$age = filter_input(INPUT_POST, <span class=\"hljs-string\">'age'<\/span>, FILTER_VALIDATE_INT, &#91;\n            <span class=\"hljs-string\">'options'<\/span> =&gt; &#91;\n                <span class=\"hljs-string\">'min_range'<\/span> =&gt; <span class=\"hljs-number\">0<\/span>,\n                <span class=\"hljs-string\">'max_range'<\/span> =&gt; <span class=\"hljs-number\">150<\/span>\n            ]\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The options limits the range of the age between 0 and 150.<\/p>\n\n\n\n<p>If the age is not valid, the <code>filter_input<\/code> function returns <code>false<\/code>. If the age is valid, then the function returns the <code>age<\/code>. <\/p>\n\n\n\n<p>Third, display an error message if the age is not valid or a success message otherwise:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>($age !== <span class=\"hljs-keyword\">false<\/span>) {\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Age is valid: \"<\/span> . htmlspecialchars($age);\n} <span class=\"hljs-keyword\">else<\/span> {\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Age is not valid:\"<\/span> . $_POST&#91;<span class=\"hljs-string\">'age'<\/span>];\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='validating-floats'>Validating floats <a href=\"#validating-floats\" class=\"anchor\" id=\"validating-floats\" title=\"Anchor for Validating floats\">#<\/a><\/h2>\n\n\n\n<p>The following form requests you to enter your weight and validate it as a float with the valid range of (0,300):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> \n\n<span class=\"hljs-keyword\">if<\/span>($_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>] === <span class=\"hljs-string\">'POST'<\/span>) {\n    <span class=\"hljs-comment\">\/\/ Check if the weight field is set and not empty<\/span>\n    <span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'weight'<\/span>)) {\n        <span class=\"hljs-comment\">\/\/ validate weight between 0 and 150<\/span>\n        $weight = filter_input(INPUT_POST, <span class=\"hljs-string\">'weight'<\/span>, FILTER_VALIDATE_FLOAT, &#91;\n            <span class=\"hljs-string\">'options'<\/span> =&gt; &#91;\n                <span class=\"hljs-string\">'min_range'<\/span> =&gt; <span class=\"hljs-number\">0<\/span>,\n                <span class=\"hljs-string\">'max_range'<\/span> =&gt; <span class=\"hljs-number\">300<\/span>\n\n            ]\n        ]);\n\n        <span class=\"hljs-keyword\">if<\/span>($weight !== <span class=\"hljs-keyword\">false<\/span>) {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Weight is valid: \"<\/span> . htmlspecialchars($weight);\n        } <span class=\"hljs-keyword\">else<\/span> {\n            <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Weight is not valid:\"<\/span> . $_POST&#91;<span class=\"hljs-string\">'weight'<\/span>];\n        }\n    } \n}\n<span class=\"hljs-meta\">?&gt;<\/span> \n\n&lt;form action=<span class=\"hljs-string\">\"&lt;?= htmlspecialchars($_SERVER&#91;'PHP_SELF']) ?&gt;\"<\/span> method=<span class=\"hljs-string\">\"post\"<\/span>&gt;\n    &lt;div&gt;\n        &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"weight\"<\/span>&gt;Weight:&lt;\/label&gt;\n        &lt;input type=<span class=\"hljs-string\">\"text\"<\/span> name=<span class=\"hljs-string\">\"weight\"<\/span> placeholder=<span class=\"hljs-string\">\"Enter your weight in lbs\"<\/span>&gt;\n        &lt;button type=<span class=\"hljs-string\">\"submit\"<\/span>&gt;Submit&lt;\/button&gt;\n    &lt;\/div&gt;\n&lt;\/form&gt;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, check if the weight is in the <code>POST<\/code> request using the <code>filter_has_var<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'weight'<\/span>)) {<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, validate the weight using the <code>filter_input<\/code> function with the filter id <code>FILTER_VALIDATE_FLOAT<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$weight = filter_input(INPUT_POST, <span class=\"hljs-string\">'weight'<\/span>, FILTER_VALIDATE_FLOAT, &#91;\n            <span class=\"hljs-string\">'options'<\/span> =&gt; &#91;\n                <span class=\"hljs-string\">'min_range'<\/span> =&gt; <span class=\"hljs-number\">0<\/span>,\n                <span class=\"hljs-string\">'max_range'<\/span> =&gt; <span class=\"hljs-number\">300<\/span>\n\n            ]\n]);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The options limits the range of the weight between 0 and 300.<\/p>\n\n\n\n<p>If the weight is not valid, the <code>filter_input<\/code> function returns <code>false<\/code>. If the weight is valid, then the function returns the <code>weight<\/code> as a float. <\/p>\n\n\n\n<p class=\"note\">Note that the filter <code>FILTER_VALIDATE_FLOAT<\/code> trims the input before validating.<\/p>\n\n\n\n<p>Third, display an error message if the weight is not valid or a success message otherwise:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> ($weight !== <span class=\"hljs-keyword\">false<\/span>) {\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Weight is valid: \"<\/span> . htmlspecialchars($weight);\n} <span class=\"hljs-keyword\">else<\/span> {\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Weight is not valid:\"<\/span> . $_POST&#91;<span class=\"hljs-string\">\"weight\"<\/span>];\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='php-form-validation-example'>PHP form validation example <a href=\"#php-form-validation-example\" class=\"anchor\" id=\"php-form-validation-example\" title=\"Anchor for PHP form validation example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll build an email <a href=\"https:\/\/phptutorial.net\/app\/newsletter\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">subscription form<\/a> that includes a validation feature. The form has the name and email input elements and a submit button:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/phptutorial.net\/app\/newsletter\/\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"358\" height=\"416\" src=\"https:\/\/phptutorial.net\/wp-content\/uploads\/2021\/04\/PHP-Form-Validation-Email-Subscription-Form.png\" alt=\"PHP form validation\" class=\"wp-image-983\" srcset=\"https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/04\/PHP-Form-Validation-Email-Subscription-Form.png 358w, https:\/\/www.phptutorial.net\/wp-content\/uploads\/2021\/04\/PHP-Form-Validation-Email-Subscription-Form-258x300.png 258w\" sizes=\"auto, (max-width: 358px) 100vw, 358px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>If you don&#8217;t enter the name and\/or email and click the subscribe button, the form will show the error messages. Also, if you enter an invalid email address, the form will show a different error message.<\/p>\n\n\n\n<p class=\"note\">Notice that we don&#8217;t use the client-side validation for this form to make it easier to test. In practice, you should also use client-side validation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='organize-the-directory-and-files'>Organize the directory and files <a href=\"#organize-the-directory-and-files\" class=\"anchor\" id=\"organize-the-directory-and-files\" title=\"Anchor for Organize the directory and files\">#<\/a><\/h3>\n\n\n\n<p>First, create a file and directory structure as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">.\n\u251c\u2500\u2500 css\n\u2502   \u2514\u2500\u2500 style.css\n\u251c\u2500\u2500 inc\n\u2502   \u251c\u2500\u2500 get.php\n\u2502   \u251c\u2500\u2500 post.php\n\u2502   \u251c\u2500\u2500 header.php\n\u2502   \u251c\u2500\u2500 footer.php\n\u2502   \u2514\u2500\u2500 .htaccess\n\u2514\u2500\u2500 index.php<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following table describes the purpose of each file:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>File<\/th><th>Directory<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>index.php<\/td><td>.<\/td><td>Contain the main logic of the form<\/td><\/tr><tr><td>header.php<\/td><td>inc<\/td><td>Contain the header code<\/td><\/tr><tr><td>footer.php<\/td><td>inc<\/td><td>Contain the footer code<\/td><\/tr><tr><td>get.php<\/td><td>inc<\/td><td>Contain the email subscription form<\/td><\/tr><tr><td>post.php<\/td><td>inc<\/td><td>Contain the code for handling form submission<\/td><\/tr><tr><td>style.css<\/td><td>css<\/td><td>Contain the CSS code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='header-php'>header.php <a href=\"#header-php\" class=\"anchor\" id=\"header-php\" title=\"Anchor for header.php\">#<\/a><\/h3>\n\n\n\n<p>The following shows the <code>header.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=<span class=\"hljs-string\">\"en\"<\/span>&gt;\n&lt;head&gt;\n    &lt;meta charset=<span class=\"hljs-string\">\"UTF-8\"<\/span>&gt;\n    &lt;meta name=<span class=\"hljs-string\">\"viewport\"<\/span> content=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span>&gt;\n    &lt;link rel=<span class=\"hljs-string\">\"stylesheet\"<\/span> href=<span class=\"hljs-string\">\"css\/style.css\"<\/span>&gt;\n    &lt;title&gt;Subscribe&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;main&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>header.php<\/code> file link to the <code>style.css<\/code> file in the <code>css<\/code> directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='footer-php'>footer.php <a href=\"#footer-php\" class=\"anchor\" id=\"footer-php\" title=\"Anchor for footer.php\">#<\/a><\/h3>\n\n\n\n<p>And the <code>footer.php<\/code> only contains the enclosing tags that correspond to the opening tags in the <code>header.php<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;\/main&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='index-php'>index.php <a href=\"#index-php\" class=\"anchor\" id=\"index-php\" title=\"Anchor for index.php\">#<\/a><\/h3>\n\n\n\n<p>The <code>index.php<\/code> file contains the main logic of the form:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/header.php'<\/span>;\n\n$errors = &#91;];\n$inputs = &#91;];\n\n$request_method = strtoupper($_SERVER&#91;<span class=\"hljs-string\">'REQUEST_METHOD'<\/span>]);\n\n<span class=\"hljs-keyword\">if<\/span> ($request_method === <span class=\"hljs-string\">'GET'<\/span>) {\n    <span class=\"hljs-comment\">\/\/ show the form<\/span>\n    <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/get.php'<\/span>;\n} <span class=\"hljs-keyword\">elseif<\/span> ($request_method === <span class=\"hljs-string\">'POST'<\/span>) {\n    <span class=\"hljs-comment\">\/\/ handle the form submission<\/span>\n    <span class=\"hljs-keyword\">require<\/span>    <span class=\"hljs-keyword\">__DIR__<\/span> .  <span class=\"hljs-string\">'\/inc\/post.php'<\/span>;\n    <span class=\"hljs-comment\">\/\/ show the form if the error exists<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (count($errors) &gt; <span class=\"hljs-number\">0<\/span>) {\n        <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> . <span class=\"hljs-string\">'\/inc\/get.php'<\/span>;\n    }\n}\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-keyword\">__DIR__<\/span> .  <span class=\"hljs-string\">'\/inc\/footer.php'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How the <code>index.php<\/code> works.<\/p>\n\n\n\n<p>First, load code from both <code>header.php<\/code> and <code>footer.php<\/code> files using the <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-require\/\">require<\/a><\/code> construct at the top and bottom of the file to generate the header and footer.<\/p>\n\n\n\n<p>Second, define the <code>$errors<\/code> array to store error messages and the <code>$inputs<\/code> array to store the entered form values. If an input element has invalid data, the <code>index.php<\/code> will show the entered value stored in the <code>$inputs<\/code>.<\/p>\n\n\n\n<p>Third, show the form if the HTTP request method is GET by loading the get.php file. Once you enter the<\/p>\n\n\n\n<p>Finally, load the code in the <code>post.php<\/code> to handle the form submission if the HTTP request method is POST. If the form has any errors, the <code>$errors<\/code> will not empty. In this case, show the form again with the error messages stored in the <code>$errors<\/code> array and entered values stored in the <code>$inputs<\/code> arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='get-php'>get.php <a href=\"#get-php\" class=\"anchor\" id=\"get-php\" title=\"Anchor for get.php\">#<\/a><\/h3>\n\n\n\n<p>The <code>get.php<\/code> file contains the form:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">&lt;form action=<span class=\"hljs-string\">\"&lt;?php echo htmlspecialchars($_SERVER&#91;'PHP_SELF']) ?&gt;\"<\/span> method=<span class=\"hljs-string\">\"post\"<\/span>&gt;\n    &lt;header&gt;\n        &lt;h1&gt;Get FREE Updates&lt;\/h1&gt;\n        &lt;p&gt;Join us <span class=\"hljs-keyword\">for<\/span> FREE to get email updates!&lt;\/p&gt;\n    &lt;\/header&gt;\n    &lt;div&gt;\n        &lt;label <span class=\"hljs-keyword\">for<\/span>=<span class=\"hljs-string\">\"name\"<\/span>&gt;Name:&lt;\/label&gt;\n        &lt;input type=<span class=\"hljs-string\">\"text\"<\/span> name=<span class=\"hljs-string\">\"name\"<\/span> id=<span class=\"hljs-string\">\"name\"<\/span> placeholder=<span class=\"hljs-string\">\"Full Name\"<\/span> value=<span class=\"hljs-string\">\"&lt;?php echo $inputs&#91;'name'] ?? '' ?&gt;\"<\/span> <span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span>=\"&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> <span class=\"hljs-title\">isset<\/span>($<span class=\"hljs-title\">errors<\/span>&#91;'<span class=\"hljs-title\">name<\/span>']) ? '<span class=\"hljs-title\">error<\/span>' : ''  ?&gt;\"&gt;\n        &lt;<span class=\"hljs-title\">small<\/span>&gt;&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> $<span class=\"hljs-title\">errors<\/span>&#91;'<span class=\"hljs-title\">name<\/span>'] ?? '' ?&gt;&lt;\/<span class=\"hljs-title\">small<\/span>&gt;\n    &lt;\/<span class=\"hljs-title\">div<\/span>&gt;\n    &lt;<span class=\"hljs-title\">div<\/span>&gt;\n        &lt;<span class=\"hljs-title\">label<\/span> <span class=\"hljs-title\">for<\/span>=\"<span class=\"hljs-title\">name<\/span>\"&gt;<span class=\"hljs-title\">Email<\/span>:&lt;\/<span class=\"hljs-title\">label<\/span>&gt;\n        &lt;<span class=\"hljs-title\">input<\/span> <span class=\"hljs-title\">type<\/span>=\"<span class=\"hljs-title\">text<\/span>\" <span class=\"hljs-title\">name<\/span>=\"<span class=\"hljs-title\">email<\/span>\" <span class=\"hljs-title\">id<\/span>=\"<span class=\"hljs-title\">email<\/span>\" <span class=\"hljs-title\">placeholder<\/span>=\"<span class=\"hljs-title\">Email<\/span> <span class=\"hljs-title\">Address<\/span>\" <span class=\"hljs-title\">value<\/span>=\"&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> $<span class=\"hljs-title\">inputs<\/span>&#91;'<span class=\"hljs-title\">email<\/span>'] ?? '' ?&gt;\" <span class=\"hljs-title\">class<\/span>=\"&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> <span class=\"hljs-title\">isset<\/span>($<span class=\"hljs-title\">errors<\/span>&#91;'<span class=\"hljs-title\">email<\/span>']) ? '<span class=\"hljs-title\">error<\/span>' : '' ?&gt;\"&gt;\n        &lt;<span class=\"hljs-title\">small<\/span>&gt;&lt;?<span class=\"hljs-title\">php<\/span> <span class=\"hljs-title\">echo<\/span> $<span class=\"hljs-title\">errors<\/span>&#91;'<span class=\"hljs-title\">email<\/span>'] ?? '' ?&gt;&lt;\/<span class=\"hljs-title\">small<\/span>&gt;\n    &lt;\/<span class=\"hljs-title\">div<\/span>&gt;\n    &lt;<span class=\"hljs-title\">button<\/span> <span class=\"hljs-title\">type<\/span>=\"<span class=\"hljs-title\">submit<\/span>\"&gt;<span class=\"hljs-title\">Subscribe<\/span>&lt;\/<span class=\"hljs-title\">button<\/span>&gt;\n&lt;\/<span class=\"hljs-title\">form<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How the <code>get.php<\/code> works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, fill the name and email input elements with the entered values stored in the <code>$inputs<\/code> array only if these values exist.<\/li>\n\n\n\n<li>Second, show the error messages stored in the <code>$errors<\/code> array if they exist.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='post-php'>post.php <a href=\"#post-php\" class=\"anchor\" id=\"post-php\" title=\"Anchor for post.php\">#<\/a><\/h3>\n\n\n\n<p>The following shows the code of the <code>post.php<\/code> file. The <code>post.php<\/code> validates the form data using the <code>filter_input()<\/code> and <code>filter_var()<\/code> functions.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> NAME_REQUIRED = <span class=\"hljs-string\">'Please enter your name'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> EMAIL_REQUIRED = <span class=\"hljs-string\">'Please enter your email'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> EMAIL_INVALID = <span class=\"hljs-string\">'Please enter a valid email'<\/span>;\n\n\n<span class=\"hljs-comment\">\/\/ sanitize and validate name<\/span>\n<span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'name'<\/span>)) {\n    $name = htmlspecialchars($_POST&#91;<span class=\"hljs-string\">'name'<\/span>]);\n    <span class=\"hljs-keyword\">if<\/span>($name === <span class=\"hljs-string\">''<\/span>) {\n        $errors&#91;<span class=\"hljs-string\">'name'<\/span>] = NAME_REQUIRED;\n    } <span class=\"hljs-keyword\">else<\/span> {\n        $inputs&#91;<span class=\"hljs-string\">'name'<\/span>] = $name;\n    }\n\n} <span class=\"hljs-keyword\">else<\/span> {\n    $errors&#91;<span class=\"hljs-string\">'name'<\/span>] = NAME_REQUIRED;\n}\n\n<span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>)) {\n    <span class=\"hljs-comment\">\/\/ sanitnize email first<\/span>\n    $email = filter_input(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>, FILTER_SANITIZE_EMAIL);\n    $inputs&#91;<span class=\"hljs-string\">'email'<\/span>] = $email;\n    <span class=\"hljs-keyword\">if<\/span>($email) {\n        <span class=\"hljs-comment\">\/\/ validate email<\/span>\n        $email = filter_var($email, FILTER_VALIDATE_EMAIL);\n        <span class=\"hljs-comment\">\/\/ email is not valid<\/span>\n        <span class=\"hljs-keyword\">if<\/span>($email === <span class=\"hljs-keyword\">false<\/span>) {\n            $errors&#91;<span class=\"hljs-string\">'email'<\/span>] = EMAIL_INVALID;\n        } \n    } <span class=\"hljs-keyword\">else<\/span> {\n        $errors&#91;<span class=\"hljs-string\">'email'<\/span>] = EMAIL_REQUIRED;\n    } \n    \n} <span class=\"hljs-keyword\">else<\/span> {\n    $errors&#91;<span class=\"hljs-string\">'email'<\/span>] = EMAIL_REQUIRED;\n}\n\n\n<span class=\"hljs-meta\">?&gt;<\/span>\n\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">if<\/span> (count($errors) === <span class=\"hljs-number\">0<\/span>) : <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;section&gt;\n        &lt;h2&gt;\n            Thanks <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> htmlspecialchars($name) <span class=\"hljs-meta\">?&gt;<\/span> <span class=\"hljs-keyword\">for<\/span> your subscription!\n        &lt;\/h2&gt;\n        &lt;p&gt;Please follow the steps below to complete your subscription:&lt;\/p&gt;\n        &lt;ol&gt;\n            &lt;li&gt;Check your email (<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> htmlspecialchars($email) <span class=\"hljs-meta\">?&gt;<\/span>) - Find the message sent from webmaster@phptutorial.net&lt;\/li&gt;\n            &lt;li&gt;Click to confirm - Click on the link in the email to confirm your subscription.&lt;\/li&gt;\n        &lt;\/ol&gt;\n    &lt;\/section&gt;\n\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endif<\/span> <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define some constants to store the error messages. In a real-world application, you can store all the messages in a separate file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">const<\/span> NAME_REQUIRED = <span class=\"hljs-string\">'Please enter your name'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> EMAIL_REQUIRED = <span class=\"hljs-string\">'Please enter your email'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> EMAIL_INVALID = <span class=\"hljs-string\">'Please enter a valid email'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, sanitize and validate the name using the <code>htmlspecialchars()<\/code> function. If the <code>name<\/code> is empty, add an error message to the <code>$errors<\/code> array.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'name'<\/span>)) {\n    $name = htmlspecialchars($_POST&#91;<span class=\"hljs-string\">'name'<\/span>]);\n    <span class=\"hljs-keyword\">if<\/span>($name === <span class=\"hljs-string\">''<\/span>) {\n        $errors&#91;<span class=\"hljs-string\">'name'<\/span>] = NAME_REQUIRED;\n    } <span class=\"hljs-keyword\">else<\/span> {\n        $inputs&#91;<span class=\"hljs-string\">'name'<\/span>] = $name;\n    }\n\n} <span class=\"hljs-keyword\">else<\/span> {\n    $errors&#91;<span class=\"hljs-string\">'name'<\/span>] = NAME_REQUIRED;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, sanitize and validate email using the <code>filter_input()<\/code> and <code>filter_var()<\/code> functions. If the email is empty or invalid, add the corresponding error message to the <code>$errors<\/code> array.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/\/ sanitize and validate email<\/span>\n<span class=\"hljs-keyword\">if<\/span>(filter_has_var(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>)) {\n    <span class=\"hljs-comment\">\/\/ sanitnize email first<\/span>\n    $email = filter_input(INPUT_POST, <span class=\"hljs-string\">'email'<\/span>, FILTER_SANITIZE_EMAIL);\n    $inputs&#91;<span class=\"hljs-string\">'email'<\/span>] = $email;\n    <span class=\"hljs-keyword\">if<\/span>($email) {\n        <span class=\"hljs-comment\">\/\/ validate email<\/span>\n        $email = filter_var($email, FILTER_VALIDATE_EMAIL);\n        <span class=\"hljs-comment\">\/\/ email is not valid<\/span>\n        <span class=\"hljs-keyword\">if<\/span>($email === <span class=\"hljs-keyword\">false<\/span>) {\n            $errors&#91;<span class=\"hljs-string\">'email'<\/span>] = EMAIL_INVALID;\n        } \n    } <span class=\"hljs-keyword\">else<\/span> {\n        $errors&#91;<span class=\"hljs-string\">'email'<\/span>] = EMAIL_REQUIRED;\n    } \n    \n} <span class=\"hljs-keyword\">else<\/span> {\n    $errors&#91;<span class=\"hljs-string\">'email'<\/span>] = EMAIL_REQUIRED;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, if the form has no error, show the confirmation message:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">if<\/span> (count($errors) === <span class=\"hljs-number\">0<\/span>) : <span class=\"hljs-meta\">?&gt;<\/span>\n    &lt;section&gt;\n        &lt;h2&gt;\n            Thanks <span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> htmlspecialchars($name) <span class=\"hljs-meta\">?&gt;<\/span> <span class=\"hljs-keyword\">for<\/span> your subscription!\n        &lt;\/h2&gt;\n        &lt;p&gt;Please follow the steps below to complete your subscription:&lt;\/p&gt;\n        &lt;ol&gt;\n            &lt;li&gt;Check your email (<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">echo<\/span> htmlspecialchars($email) <span class=\"hljs-meta\">?&gt;<\/span>) - Find the message sent from webmaster@phptutorial.net&lt;\/li&gt;\n            &lt;li&gt;Click to confirm - Click on the link in the email to confirm your subscription.&lt;\/li&gt;\n        &lt;\/ol&gt;\n    &lt;\/section&gt;\n<span class=\"hljs-meta\">&lt;?php<\/span> <span class=\"hljs-keyword\">endif<\/span> <span class=\"hljs-meta\">?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To complete the form, you can save the contact data to a database or call an API of an email marketing service to add the contact to your list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>filter_has_var()<\/code> function to check if variables exists in the <code>INPUT_POST<\/code>.<\/li>\n\n\n\n<li>Use the <code>filter_input()<\/code> function to sanitize and validate data.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"979\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-form-validation\/\"\n\t\t\t\tdata-post-title=\"PHP Form Validation\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"979\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-form-validation\/\"\n\t\t\t\tdata-post-title=\"PHP Form Validation\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about PHP form validation and show error messages if the user inputs are invalid.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":86,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-979","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=979"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/979\/revisions"}],"predecessor-version":[{"id":3210,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/979\/revisions\/3210"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}