{"id":344,"date":"2018-01-11T14:19:39","date_gmt":"2018-01-11T14:19:39","guid":{"rendered":"http:\/\/mddir.com\/how-to\/?p=344"},"modified":"2018-01-11T14:19:39","modified_gmt":"2018-01-11T14:19:39","slug":"validate-form-data-with-php","status":"publish","type":"post","link":"https:\/\/www.mddir.com\/how-to\/validate-form-data-with-php\/","title":{"rendered":"Validate Form Data With PHP"},"content":{"rendered":"<p>Validating form data with php is essential to avoid security issue with your website. The hacker can use your form to input the virus or the script to hack important data from your website.<\/p>\n<p>Let check the Html form that validates your form details.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>[php]&lt;?php<br \/>\n\/\/ define variables and set to empty values<br \/>\n$name = $email = $gender = $comment = $website = &quot;&quot;;<\/p>\n<p>if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {<br \/>\n  $name = test_input($_POST[&quot;name&quot;]);<br \/>\n  $email = test_input($_POST[&quot;email&quot;]);<br \/>\n  $website = test_input($_POST[&quot;website&quot;]);<br \/>\n  $comment = test_input($_POST[&quot;comment&quot;]);<br \/>\n  $gender = test_input($_POST[&quot;gender&quot;]);<br \/>\n}<\/p>\n<p>function test_input($data) {<br \/>\n  $data = trim($data);<br \/>\n  $data = stripslashes($data);<br \/>\n  $data = htmlspecialchars($data);<br \/>\n  return $data;<br \/>\n}<br \/>\n?&gt;[\/php]<\/p>\n<h2>PHP Form Validation Example<\/h2>\n<p>[php]&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER[&quot;PHP_SELF&quot;]);?&gt;&quot;&gt;<br \/>\n  Name: &lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;<br \/>\n  &lt;br&gt;&lt;br&gt;<br \/>\n  E-mail: &lt;input type=&quot;text&quot; name=&quot;email&quot;&gt;<br \/>\n  &lt;br&gt;&lt;br&gt;<br \/>\n  Website: &lt;input type=&quot;text&quot; name=&quot;website&quot;&gt;<br \/>\n  &lt;br&gt;&lt;br&gt;<br \/>\n  Comment: &lt;textarea name=&quot;comment&quot; rows=&quot;5&quot; cols=&quot;40&quot;&gt;&lt;\/textarea&gt;<br \/>\n  &lt;br&gt;&lt;br&gt;<br \/>\n  Gender:<br \/>\n  &lt;input type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;female&quot;&gt;Female<br \/>\n  &lt;input type=&quot;radio&quot; name=&quot;gender&quot; value=&quot;male&quot;&gt;Male<br \/>\n  &lt;br&gt;&lt;br&gt;<br \/>\n  &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;<br \/>\n&lt;\/form&gt;[\/php]<\/p>\n<p>[php]&lt;?php<br \/>\necho &quot;&lt;h2&gt;Your Input:&lt;\/h2&gt;&quot;;<br \/>\necho $name;<br \/>\necho &quot;&lt;br&gt;&quot;;<br \/>\necho $email;<br \/>\necho &quot;&lt;br&gt;&quot;;<br \/>\necho $website;<br \/>\necho &quot;&lt;br&gt;&quot;;<br \/>\necho $comment;<br \/>\necho &quot;&lt;br&gt;&quot;;<br \/>\necho $gender;<br \/>\n?&gt;[\/php]<\/p>\n<p>In the above example, we are using PHP_SELP method to get the data of the php. When form executed the user will see the details on the same page.<\/p>\n<p>In the php function first step is to define the variable with empty values.<br \/>\n<code><br \/>\n$name = $email = $gender = $comment = $website = \"\";<\/code><\/p>\n<p>Next we will be using if statement to assign the request method at post value. In this code we have added each variable to its html form value and we have given <code>test_input<\/code> a function name to the value.<\/p>\n<p>[php]if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {<br \/>\n  $name = test_input($_POST[&quot;name&quot;]);<br \/>\n  $email = test_input($_POST[&quot;email&quot;]);<br \/>\n  $website = test_input($_POST[&quot;website&quot;]);<br \/>\n  $comment = test_input($_POST[&quot;comment&quot;]);<br \/>\n  $gender = test_input($_POST[&quot;gender&quot;]);<br \/>\n}[\/php]<\/p>\n<p>After that, we will be verifying the user&#8217;s input data with the php functions. To do that we will be running our function <code>text_input($data)<\/code> through the security check php functions as given below.<\/p>\n<p>[php][\/php]function test_input($data) {<br \/>\n  $data = trim($data);<br \/>\n  $data = stripslashes($data);<br \/>\n  $data = htmlspecialchars($data);<br \/>\n  return $data;<br \/>\n}<\/p>\n<p>The php function <code>trim()<\/code> is actually trims the data by removing unwanted space from the user&#8217;s input.<\/p>\n<p>The function <code>stripslashes()<\/code> removes the lash from the input data.<\/p>\n<p>The function <code>htmlspecialchars()<\/code> replace the special characters to the HTML format.<\/p>\n<p>In the end, we get the result of the users on the page. You need to use echo statement to get the form details to present on the page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Validating form data with php is essential to avoid security issue with your website. The hacker can use your form to input the virus or<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/comments?post=344"}],"version-history":[{"count":1,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/344\/revisions"}],"predecessor-version":[{"id":345,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/posts\/344\/revisions\/345"}],"wp:attachment":[{"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/media?parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/categories?post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mddir.com\/how-to\/wp-json\/wp\/v2\/tags?post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}