Firstly, if you haven't already you will need to read the $_GET and $_POST tutorials. Without reading those tutorials first this tutorial will make very little sense.
So, firstly what is form validation? Well, basically it's getting the user input and making sure it's as you require. For example you don't want someone putting in a password that is less than you specified. Below are some useful methods that you can use to help validate input:
String Length:
Code :
Output:5
An IF statement:
Code :
Output:The passwords do not match!
Integer Checking:
Code :
Output:You entered a number!
Check for a character(Email verification):
Code :
Output:No @ sign was found!
So, you have what you need to validate input. Below is a fully functioning form, you can test out the form here. Below the code is an explanation of new code.
Code :PHP Form Validation Example
This might look scary at first - there is quite a lot of code. Currently this form isn't that useful, it would normally submit the information to a database but that will be covered in later tutorials. Other than two or three lines of code most of the stuff has been previously covered. Below are explanations of the new parts:
Code :
The above line of code will increment the value stored in the variable. It is a simpler way of adding 1 to a value.
Code :
These two lines of codes are used on almost every page with a form. They check to see if the form has been submitted. The first line will check if the form has been submitted, any code you want to run when the form is submitted should be placed between the two curly brackets. The second line does the opposite, it checks to see if the form hasn't been submitted. In this case it simply displays the form.
If you are wondering what to do if your button is not called 'submit' you simply replace the $_POST['thispart'] part between the two apostrophes. As I mentioned before you can check out the working code here.
To post comments you must sign in to your account below!
If you do not currently have an account click here to create one now!