HOW TO VALIDATE FORM INPUTS BEFORE SUBMISSION USING PHP.

in #howto7 years ago

what you would learn

  • You will learn how to use preg_match function to search strings for required pattern
  • You will learn how to use filter_var function to check valid email entry
  • You will learn how to check if an entered input like email already exist in a database.

Requirements

  • A text edditor
  • An apache and SQL sever
  • A browser

Tutorial Contents

You might have a form on your website and you want to make sure that users fill in the required information before submission
First you must have created a form page and it looks like this

form1.png

output should look like this
form2.png

Next you create a div class that contains the error message and style it to color red it looks like this
form3.png

Then you write the PHP code for validation, here we use pregmatch, test_input and strlen.
Note: we are putting our PHP code in the same page as our form then use the PHP_SELF super global
Create a data base and a table that collects the information from the form so let’s use database “test” and table “validate” take a look at the table

form4.png

Now put your action attribute and use “($_SERVER["PHP_SELF"])” with method as “post” then call for the error message by putting this code “<?php echo $theError ;” between the div tags with class error it looks like this

form5.png

Now the PHP codes.
First you create a connection to the database in use “test”

form6.png

Then you create some variables and equate them to zero, these variable would be assigned values along the way. Then as you can see the first “if” statement asks if the request method was post if yes then the validation starts.

form7.png

Using preg_match to reject name inputs that are not letters and white space

form8.png

Using filter_var to accept only inputs in email form, e.g. [email protected] then create variables $check and $check2. The variable $check select’s email from table “validate” with a where condition for checking a matched email, then $check2 checks if there was a row selected by $check if any it returns an error “email already exist”.

Here Preg_match accepts password inputs with letters and numbers only

form9.png

Now on clicking the submit button “(isset($_POST[‘submit’]))” the function strlen checks if an error (any of $emailErr, $nameErr or $passwordErr) occurred if any it returns a message “some error occurred” if none then the insert query inserts the records to the table.

form10.png

Take a look at these results

form12.png
form13.png
form15.png

The end!

thank you for reading through.

send your suggestions and questions through comments.

Sort:  

You got a 0.98% upvote from @postpromoter courtesy of @sammyutd!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.029
BTC 69432.76
ETH 2492.61
USDT 1.00
SBD 2.53