{"id":1891,"date":"2014-12-08T14:47:08","date_gmt":"2014-12-08T12:47:08","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=1891"},"modified":"2018-06-20T15:16:31","modified_gmt":"2018-06-20T12:16:31","slug":"php-form-validation-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/","title":{"rendered":"PHP Form Validation Example"},"content":{"rendered":"<p><strong>EDITORIAL NOTE<\/strong>: In this post, we feature a comprehensive PHP Form Validation Example.<\/p>\n<p>Data is an integral part of an application. There is a golden rule for developers.<\/p>\n<blockquote><p>Never trust your user&#8217;s information.<\/p><\/blockquote>\n<p>So, we need to always filter and validate the user entered before storing it in application&#8217;s database. In this article, we will learn how to perform basic validation of form in php.<\/p>\n<p>We will create a simple registration form which will contain various\u00a0inputs like textbox, checkbox, radio buttons and a submit button. After submitting the form, we will validate the fields with php and show error messages if there are some errors.<\/p>\n<p>[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<\/p>\n<h1>Contact Form<\/h1>\n<figure id=\"attachment_1942\" aria-describedby=\"caption-attachment-1942\" style=\"width: 532px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form2.jpg\"><img decoding=\"async\" class=\"wp-image-1942 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form2.jpg\" alt=\"The Registration php form validation\" width=\"532\" height=\"617\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form2.jpg 532w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form2-258x300.jpg 258w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/a><figcaption id=\"caption-attachment-1942\" class=\"wp-caption-text\">The Registration Form<\/figcaption><\/figure>\n<h1>Text Fields<\/h1>\n<p>The name and email are the text <code>input<\/code> elements of the form and can be coded like this :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input class=&quot;input&quot; type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot;&gt;\r\n\r\n&lt;input class=&quot;input&quot; type=&quot;text&quot; name=&quot;email&quot; value=&quot;&quot;&gt;\r\n\r\n<\/pre>\n<p>The basic text input is created using the\u00a0<code>input<\/code> tag by setting the <code>type<\/code> to\u00a0<code>text<\/code>. The initial contents of the\u00a0<code>input<\/code> can be specified using the\u00a0<code>value<\/code> attribute. If you do not include this attribute, or if you specify an empty <code>value<\/code>, there will be no contents by default. The text input\u00a0is a single line input accepting any normal text characters, but not linebreaks. If the contents are too large to fit, the\u00a0input will scroll, usually without a visible scrollbar.<\/p>\n<h1>Radio Buttons<\/h1>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;PHP&quot;&gt; PHP\r\n\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;HTML&quot;&gt; HTML\r\n\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;PYTHON&quot;&gt; Python\r\n\r\n<\/pre>\n<p>A radio button is created using the <code>input<\/code>\u00a0element, by setting the <code>type<\/code>\u00a0attribute to <code>radio<\/code>. Radio buttons are put into groups by giving them the same\u00a0<code>name<\/code>\u00a0attribute, and you can have multiple radio groups in a <code>form<\/code>. To pre-select a radio input, set the <code>checked<\/code>\u00a0attribute on the desired input (this is another attribute that does not need a value). You should never attempt to pre-select more than one radio input in a group.<\/p>\n<h1>Textarea<\/h1>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;textarea name=&quot;message&quot; val=&quot;&quot;&gt;&lt;\/textarea&gt;\r\n\r\n<\/pre>\n<p>A <code>textarea<\/code> is a larger version of a <code>text<\/code> <code>input<\/code> that can contain multiple lines of text, including linebreaks. Most browsers will display a scrollbar if the contents are too large for the <code>textarea<\/code>.<\/p>\n<p>Textareas are created using the <code>textarea<\/code> tag. This requires a closing tag, and has no <code>value<\/code> attribute. Instead, the default value is written in between the opening and closing tags. It requires that you provide the <code>rows<\/code> and <code>cols<\/code> attributes, which give a suggestion for an initial size (based on the number of characters that should be displayed vertically and horizontally). This is a little unusual, since it forces you to specify display related information in the HTML, but you can always override them using the <code>height<\/code> and <code>width<\/code> styles in CSS.<\/p>\n<h1>File Input<\/h1>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input class=&quot;input&quot; type=&quot;file&quot; name=&quot;resume&quot;&gt;\r\n\r\n<\/pre>\n<p>This allows the user to choose a file that will be uploaded to the server. There are obvious security issues here, so in order to prevent pages uploading files without permission, browsers will not allow HTML or script to set the initial value of the input.<\/p>\n<p>When you make a <code>POST<\/code> request, you have to encode the data that forms the body of the request in some way.<\/p>\n<p>HTML <code>form<\/code> provide three methods of encoding.<\/p>\n<ul>\n<li><code>application\/x-www-form-urlencoded<\/code> (the default)<\/li>\n<li><code>multipart\/form-data<\/code><\/li>\n<li><code>text\/plain<\/code><\/li>\n<\/ul>\n<p>The specifics of the formats don&#8217;t matter to most developers. The important points are :<\/p>\n<p><code>application\/x-www-form-urlencoded<\/code> is more or less the same as a query string on the end of the URL.<\/p>\n<p><code>multipart\/form-data<\/code> is significantly more complicated but it allows entire files to be included in the data. An example of the result can be found in the <a href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/forms.html#h-17.13.4\">HTML 4 specification<\/a>.<\/p>\n<p><code>text\/plain<\/code> is introduced by HTML and is useful only for debugging \u2014 from <a href=\"http:\/\/www.w3.org\/TR\/html5\/forms.html#text\/plain-encoding-algorithm\">the spec<\/a>: <em>They are not reliably interpretable by computer<\/em> \u2014 and I&#8217;d argue that the others combined with tools (like the Net tab in the developer tools of most browsers) are better for that).<\/p>\n<h1>Submit Button<\/h1>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input class=&quot;submit&quot; type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;\r\n\r\n<\/pre>\n<p>The <code>submit<\/code> button is the button that causes the information in the <code>form<\/code> to be submitted to the server. Normally these are not given a <code>name<\/code> but if they are, then their <code>name<\/code> and <code>value<\/code> will also be sent to the server. This can be useful if you want to have multiple submit buttons, and you want the server side script to make decisions based on which button is clicked.<\/p>\n<p>A submit button is created using the <code>input<\/code> element, by setting the <code>type<\/code> attribute to <code>submit<\/code>.<\/p>\n<h1>Form element<\/h1>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER&#x5B;&quot;PHP_SELF&quot;]);?&gt;&quot; enctype=&quot;multipart\/form-data&quot; &gt;\r\n\r\n<\/pre>\n<p><code>htmlspecialchars()<\/code> is enough to prevent document-creation-time HTML injection with the limitations you state (ie no injection into tag content\/unquoted attribute). This prevents our form cross site scripting attacks or XSS attacks. It is always better to be on the safer side.<\/p>\n<p>Forms are defined using the form tag. There are two types of method by which you can submit your form. GET method shows every bit of information of your form in url and has limitation of 4 KB of data. In POST method, the information is hidden from the end user and you can have more than 4 KB of data.<\/p>\n<p>The action attribute defines the name of page where the page should go to submit the form contents. In the above example we the page is submitting the contents of the form to itself.<\/p>\n<h1>Form\u00a0Processing<\/h1>\n<p>As we are submitting the form to itself, we need to first detect the form submission. If we detect a form submission, then we validate the form. We detect whether the form is submitted by writing following code :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php\r\n\r\nif ( isset ( $_POST&#x5B;'submit'] ) ) { \/\/ Checking null values in message.\r\n\r\n\/\/Validate the form\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n<\/pre>\n<p>As shown in above code snippet, we first confirm whether the form has been submitted by checking if <code>submit<\/code> has been set. <code>isset<\/code> function in php checks if a variable is set and is not null.<\/p>\n<h1>Form\u00a0Validation<\/h1>\n<p>After submitting the form should throw validation errors. It looks like this :<\/p>\n<figure id=\"attachment_1927\" aria-describedby=\"caption-attachment-1927\" style=\"width: 544px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form-error2.jpg\"><img decoding=\"async\" class=\"wp-image-1927 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form-error2.jpg\" alt=\"php form validation-error\" width=\"544\" height=\"680\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form-error2.jpg 544w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form-error2-240x300.jpg 240w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/a><figcaption id=\"caption-attachment-1927\" class=\"wp-caption-text\">Form Error<\/figcaption><\/figure>\n<p>Let&#8217;s look at the php required for validating the form :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php\r\n\r\n$name =&quot;&quot;; \/\/ Sender Name\r\n\r\n$email =&quot;&quot;; \/\/ Sender's email ID\r\n\r\n$message =&quot;&quot;; \/\/ Sender's Message\r\n\r\n$nameError =&quot;&quot;;\r\n\r\n$emailError =&quot;&quot;;\r\n\r\n$messageError =&quot;&quot;;\r\n\r\n$fileError =&quot;&quot;;\r\n\r\n$successMessage =&quot;&quot;; \/\/ On submitting form below function will execute.\r\n\r\nif ( isset ( $_POST&#x5B;'submit'] ) ) \u00a0{ \/\/ Checking null values in message.\r\n\r\nif ( empty ( $_POST&#x5B;&quot;name&quot;] ) ) {\r\n\r\n$nameError = &quot;Name is required&quot;;\r\n\r\n}\r\n\r\nif ( empty ( $_POST&#x5B;&quot;email&quot;] ) ) {\r\n\r\n$emailError = &quot;Email is required&quot;;\r\n\r\n}\r\nif \u00a0 (!isset ( $_POST&#x5B;&quot;technologies&quot;] ) ) {\r\n\r\n$technologiesError = &quot;Select one technology&quot;;\r\n\r\n}\r\nif ( empty ( $_POST&#x5B;&quot;message&quot;] ) ) {\r\n\r\n$messageError = &quot;Message is required&quot;;\r\n\r\n}\r\n\r\nif ( !$_FILES&#x5B;'resume']&#x5B;'name'] ) {\r\n $fileError = &quot;File is required&quot;;\r\n }\r\n\r\n}\r\n\r\n?&gt;\r\n\r\n<\/pre>\n<p>The validation starts with following steps :<\/p>\n<ul>\n<li>A variable for each text fields<\/li>\n<li>Error message variable for each fields<\/li>\n<li>A variable for success message<\/li>\n<\/ul>\n<p><code>empty()<\/code> function usage is to determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals <strong><code>FALSE<\/code><\/strong>.<\/p>\n<p><code>empty()<code> function is used to determine whether the values are empty or not.<\/code><\/code><\/p>\n<p><code>$_FILES<\/code> is an\u00a0associative <span class=\"type\"><a class=\"type array\" href=\"http:\/\/php.net\/manual\/en\/language.types.array.php\">array<\/a><\/span> of items uploaded to the current script via the HTTP POST method.<\/p>\n<p>Also, we need to display the error messages in the form. The html code we write for displaying the error messages is :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER&#x5B;&quot;PHP_SELF&quot;]);?&gt;&quot; enctype=&quot;multipart\/form-data&quot;&gt;\r\n\r\n&lt;label&gt;Name :&lt;\/label&gt;\r\n&lt;input class=&quot;input&quot; type=&quot;text&quot; name=&quot;name&quot; value=&quot;&quot;&gt;\r\n&lt;div class=&quot;error&quot;&gt;&lt;?php echo $nameError;?&gt;&lt;\/div&gt;\r\n\r\n&lt;label&gt;Email :&lt;\/label&gt;\r\n&lt;input class=&quot;input&quot; type=&quot;text&quot; name=&quot;email&quot; value=&quot;&quot;&gt;\r\n&lt;div class=&quot;error&quot;&gt;&lt;?php echo $emailError;?&gt;&lt;\/div&gt;\r\n\r\n&lt;label&gt;Technologies :&lt;\/label&gt;\r\n&lt;div&gt;\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;PHP&quot;&gt; PHP\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;HTML&quot;&gt; HTML\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;PYTHON&quot;&gt; Python\r\n&lt;\/div&gt;\r\n&lt;div class=&quot;error&quot;&gt;&lt;?php echo $technologiesError;?&gt;&lt;\/div&gt;\r\n\r\n&lt;label&gt;Message :&lt;\/label&gt;\r\n&lt;textarea name=&quot;message&quot; val=&quot;&quot;&gt;&lt;\/textarea&gt;\r\n&lt;div class=&quot;error&quot;&gt;&lt;?php echo $messageError;?&gt;&lt;\/div&gt;\r\n\r\n&lt;label&gt;Resume :&lt;\/label&gt;\r\n&lt;input class=&quot;input&quot; type=&quot;file&quot; name=&quot;resume&quot;&gt;\r\n&lt;div class=&quot;error&quot;&gt;&lt;?php echo $fileError;?&gt;&lt;\/div&gt;\r\n\r\n&lt;input class=&quot;submit&quot; type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;\r\n&lt;div class=&quot;success&quot;&gt;&lt;?php echo $successMessage;?&gt;&lt;\/div&gt;\r\n&lt;\/form&gt;\r\n\r\n<\/pre>\n<p>For enhanced security measures, we can have <code>htmlspecialchars()<\/code> processing before outputting the input to form.<\/p>\n<h1>Extending Form&#8217;s validation rules<\/h1>\n<p>For now, we are just checking whether the input is empty or not. But, we need to validate whether the email entered by user is valid or not. We alter our validation code for email as follows :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\nif ( ! filter_var ( $_POST&#x5B;&quot;email&quot;] , FILTER_VALIDATE_EMAIL ) )\r\n{\r\n$emailError = &quot;Email is not valid&quot;;\r\n}\r\n\r\n<\/pre>\n<p><code>filter_var<\/code> will do, both, sanitize and validate data. What&#8217;s the difference between the two?<\/p>\n<ul>\n<li>Sanitizing will remove any illegal character from the data.<\/li>\n<li>Validating will determine if the data is in proper form.<\/li>\n<\/ul>\n<p><em>Note:<\/em> why sanitize and not just validate? It&#8217;s possible the user accidentally typed in a wrong character or maybe it was from a bad copy and paste. By sanitizing the data, you take the responsibility of hunting for the mistake off of the user.<\/p>\n<p>So, now we can now sanitize and validate user&#8217;s email address.<\/p>\n<h1>Extending Form&#8217;s\u00a0usability<\/h1>\n<p>We now validated the form, but our input data is nor being repopulated and this will irritate user a lot. We need to now add functionality to repopulate the form data.<\/p>\n<p>In our php validation snippet :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\nif ( isset ( $_POST&#x5B;'submit'] ) ) { \/\/ Checking null values in message.\r\n\r\n$name = $_POST&#x5B;&quot;name&quot;]; \/\/ Sender Name\r\n$email = $_POST&#x5B;&quot;email&quot;]; \/\/ Sender's email ID\r\n$message = $_POST&#x5B;&quot;message&quot;]; \/\/ Sender's Message\r\n\r\n<\/pre>\n<p>While some changes in our html form :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input class=&quot;input&quot; type=&quot;text&quot; name=&quot;name&quot; value=&quot;&lt;?php echo $name; ?&gt;&quot;&gt;\r\n\r\n&lt;input class=&quot;input&quot; type=&quot;text&quot; name=&quot;email&quot; value=&quot;&lt;?php echo $email; ?&gt;&quot;&gt;\r\n\r\n&lt;textarea name=&quot;message&quot; val=&quot;&quot;&gt;&lt;?php echo $message; ?&gt;&lt;\/textarea&gt;\r\n\r\n<\/pre>\n<p>So, now our form&#8217;s data is being repopulated and user irritates a lot lesser. But, still we haven&#8217;t being able to repopulate our selection for radio button. With a bit more effort and functionality we can achieve it as follows :<\/p>\n<p>In our form processing in PHP we store value for technologies as well.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\nif(isset($_POST&#x5B;'submit'])) { \/\/ Checking null values in message.\r\n\r\n$name = $_POST&#x5B;&quot;name&quot;]; \/\/ Sender Name\r\n\r\n$email = $_POST&#x5B;&quot;email&quot;]; \/\/ Sender's email ID\r\n\r\n$message = $_POST&#x5B;&quot;message&quot;]; \/\/ Sender's Message\r\n\r\n$technologies = $_POST&#x5B;&quot;technologies&quot;]; \/\/selected value of technologies\r\n\r\n}\r\n\r\n<\/pre>\n<p>We have to also modify our HTML for <code>radio<\/code> buttons to select them again. So, we do :<\/p>\n<ul>\n<li>Check if <code>$technologies<\/code> is previously set<\/li>\n<li>If it is set and the <code>value<\/code> is matching current <code>radio<\/code> button value then we <code>echo<\/code> <code>checked<\/code><\/li>\n<\/ul>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;PHP&quot; &lt;?php if (isset($technologies) &amp;&amp; $technologies == &quot;PHP&quot;) echo &quot;checked&quot;; ?&gt; &gt; PHP\r\n\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;HTML&quot; &lt;?php if (isset($technologies) &amp;&amp; $technologies == &quot;HTML&quot;) echo &quot;checked&quot;; ?&gt; &gt; HTML\r\n\r\n&lt;input type=&quot;radio&quot; name=&quot;technologies&quot; value=&quot;PYTHON&quot; &lt;?php if (isset($technologies) &amp;&amp; $technologies == &quot;PYTHON&quot;) echo &quot;checked&quot;; ?&gt; &gt; Python\r\n\r\n<\/pre>\n<p>So, if we check now, our radio buttons are re-selected even if there are errors :<\/p>\n<figure id=\"attachment_1934\" aria-describedby=\"caption-attachment-1934\" style=\"width: 532px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Reselect-radio-Button.jpg\"><img decoding=\"async\" class=\"wp-image-1934 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Reselect-radio-Button.jpg\" alt=\"Reselected radio button php form validation\" width=\"532\" height=\"617\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Reselect-radio-Button.jpg 532w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Reselect-radio-Button-258x300.jpg 258w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/a><figcaption id=\"caption-attachment-1934\" class=\"wp-caption-text\">Reselected radio button<\/figcaption><\/figure>\n<h1>Displaying success message<\/h1>\n<p>We have handled form validation and error messages. But, now we need to also handle the situation when everything is fine and user has entered all the information. We\u00a0modify our code as follows :<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$errors = 0;\r\n\r\nif(isset($_POST&#x5B;'submit'])) { \/\/ Checking null values in message.\r\n\r\n if (empty($_POST&#x5B;&quot;name&quot;])){\r\n $nameError = &quot;Name is required&quot;;\r\n $errors = 1;\r\n }\r\n\r\n if(!filter_var($_POST&#x5B;&quot;email&quot;], FILTER_VALIDATE_EMAIL))\r\n {\r\n $emailError = &quot;Email is not valid&quot;;\r\n $errors = 1;\r\n }\r\n\r\n if (!isset($_POST&#x5B;&quot;technologies&quot;])){\r\n $technologiesError = &quot;Select one technology&quot;;\r\n $errors = 1;\r\n }\r\n\r\n if (empty($_POST&#x5B;&quot;message&quot;])){\r\n $messageError = &quot;Message is required&quot;;\r\n $errors = 1;\r\n }\r\n\r\n if(!$_FILES&#x5B;'resume']&#x5B;'name']) {\r\n $fileError = &quot;File is required&quot;;\r\n $errors = 1;\r\n }\r\nif($errors == 0)\r\n{\r\n$successMessage =&quot;Form Submitted successfully...&quot;; \/\/ IF no errors set successMessage\r\n}\r\n} \r\n\r\n<\/pre>\n<p>The above code does following things :<\/p>\n<ul>\n<li>It checks whether any error is there by checking the value of <code>$errors<\/code><\/li>\n<li>If there is no error it sets the <code>$successMessage<\/code><\/li>\n<\/ul>\n<p>We display this <code>$successMessage<\/code> variable in our HTML as follows :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;input class=&quot;submit&quot; type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;\r\n\r\n&lt;div style=&quot;background-color:green&quot;&gt;&lt;?php echo $successMessage;?&gt;&lt;\/div&gt;\r\n\r\n<\/pre>\n<p>The output for above code is :<\/p>\n<figure id=\"attachment_1936\" aria-describedby=\"caption-attachment-1936\" style=\"width: 532px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Success.jpg\"><img decoding=\"async\" class=\"wp-image-1936 size-full\" src=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Success.jpg\" alt=\"php form validation Success \" width=\"532\" height=\"617\" srcset=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Success.jpg 532w, https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Success-258x300.jpg 258w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/a><figcaption id=\"caption-attachment-1936\" class=\"wp-caption-text\">Form Success<\/figcaption><\/figure>\n<p>So, we have successfully validated and submitted the form.<\/p>\n<h1>Summary<\/h1>\n<p>This is a basic form handling approach that should be followed while writing procedural php code. But, this is not the recommended approach for large scale web apps. We have many mature frameworks available to handle all these functionalities and abstract them. If you want ot go and develop a app, go for some mature framework. Procedural way of coding is not the recommended way to do any more&#8230;<\/p>\n<h2>Download the source code<\/h2>\n<p>This was an example of how to perform form validation with PHP.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/12\/Form-Validation.zip\"><strong>Form Validation Files<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>EDITORIAL NOTE: In this post, we feature a comprehensive PHP Form Validation Example. Data is an integral part of an application. There is a golden rule for developers. Never trust your user&#8217;s information. So, we need to always filter and validate the user entered before storing it in application&#8217;s database. In this article, we will &hellip;<\/p>\n","protected":false},"author":32,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP Form Validation Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"Interested to learn more about php form validation? Check out our Example where we will learn how to perform basic validation of form in php!\" \/>\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.webcodegeeks.com\/php\/php-form-validation-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP Form Validation Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about php form validation? Check out our Example where we will learn how to perform basic validation of form in php!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-08T12:47:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-20T12:16:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Viraj Khatavkar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Viraj Khatavkar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/\"},\"author\":{\"name\":\"Viraj Khatavkar\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/fdf1fff18ce41b5afa272af1e2590dda\"},\"headline\":\"PHP Form Validation Example\",\"datePublished\":\"2014-12-08T12:47:08+00:00\",\"dateModified\":\"2018-06-20T12:16:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/\"},\"wordCount\":2494,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/\",\"name\":\"PHP Form Validation Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2014-12-08T12:47:08+00:00\",\"dateModified\":\"2018-06-20T12:16:31+00:00\",\"description\":\"Interested to learn more about php form validation? Check out our Example where we will learn how to perform basic validation of form in php!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Form Validation Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/fdf1fff18ce41b5afa272af1e2590dda\",\"name\":\"Viraj Khatavkar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4b7128884081cee0e56ad1db74315b792ddda53451beaa2669bcef6b6c9d2553?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4b7128884081cee0e56ad1db74315b792ddda53451beaa2669bcef6b6c9d2553?s=96&d=mm&r=g\",\"caption\":\"Viraj Khatavkar\"},\"description\":\"Viraj has graduated from University of Mumbai, the financial heart of INDIA. Being a web geek &amp; having entrepreneurship in DNA; happens to be the Co-Founder of Tantra-Gyan (I) Business Solutions, heading Technology space. He loves PHP to the core and is great enthusiast for mastering the same. He prefers designing architectural planning of an application and researching new architectural solutions. He has developed various solutions logical as well as architectural for service industry web ERPs and Scraping apps. He is fascinated about SOA, web services and believes \\\"Good design increases software life and your leisure time as well :)\\\" Loves spending time for creating solutions for the betterment of society namely focusing on Education, Employment &amp; Civic Rights. Buzz him any moment, if you feel like; he would love to hear from you.\",\"sameAs\":[\"http:\/\/www.webcodegeeks.com\/\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/viraj-khatavkar\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP Form Validation Example - Web Code Geeks - 2026","description":"Interested to learn more about php form validation? Check out our Example where we will learn how to perform basic validation of form in php!","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.webcodegeeks.com\/php\/php-form-validation-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP Form Validation Example - Web Code Geeks - 2026","og_description":"Interested to learn more about php form validation? Check out our Example where we will learn how to perform basic validation of form in php!","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2014-12-08T12:47:08+00:00","article_modified_time":"2018-06-20T12:16:31+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Viraj Khatavkar","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Viraj Khatavkar","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/"},"author":{"name":"Viraj Khatavkar","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/fdf1fff18ce41b5afa272af1e2590dda"},"headline":"PHP Form Validation Example","datePublished":"2014-12-08T12:47:08+00:00","dateModified":"2018-06-20T12:16:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/"},"wordCount":2494,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/","name":"PHP Form Validation Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2014-12-08T12:47:08+00:00","dateModified":"2018-06-20T12:16:31+00:00","description":"Interested to learn more about php form validation? Check out our Example where we will learn how to perform basic validation of form in php!","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-form-validation-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP Form Validation Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/fdf1fff18ce41b5afa272af1e2590dda","name":"Viraj Khatavkar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4b7128884081cee0e56ad1db74315b792ddda53451beaa2669bcef6b6c9d2553?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b7128884081cee0e56ad1db74315b792ddda53451beaa2669bcef6b6c9d2553?s=96&d=mm&r=g","caption":"Viraj Khatavkar"},"description":"Viraj has graduated from University of Mumbai, the financial heart of INDIA. Being a web geek &amp; having entrepreneurship in DNA; happens to be the Co-Founder of Tantra-Gyan (I) Business Solutions, heading Technology space. He loves PHP to the core and is great enthusiast for mastering the same. He prefers designing architectural planning of an application and researching new architectural solutions. He has developed various solutions logical as well as architectural for service industry web ERPs and Scraping apps. He is fascinated about SOA, web services and believes \"Good design increases software life and your leisure time as well :)\" Loves spending time for creating solutions for the betterment of society namely focusing on Education, Employment &amp; Civic Rights. Buzz him any moment, if you feel like; he would love to hear from you.","sameAs":["http:\/\/www.webcodegeeks.com\/"],"url":"https:\/\/www.webcodegeeks.com\/author\/viraj-khatavkar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=1891"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/1891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=1891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=1891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=1891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}