In the last session we learned a simple validation process, today we will improve it and also learn to use and validate a URL in this PHP FORM URL.

  • We had already learned to accept user input through HTML form. So let us try to take some more values in a new form.
  • Let us create a Registration form.
  • The html code is given below:
  • In the above code we have a html Registration form that consists of fields like name, username, password, confirm password, city, state, mobile number, gender, email-id and web url.
  • The fields name, username, password, confirm password, city, state, mobile number, gender and email-id are required fields.
  • When these fields are filled and the REGISTER ME button is clicked, the whole data is stored in the $_POST superglobal array variable and validated.
  • The validation code is given below:
  • It is checked that if the information filled in the form is submitted using
  • If the data is submitted all the values stored in the $_POST superglobal array variable are stored in the respective local variables.
  • A variable $valid is declared and set to false to confirm that the whole form is validated. I mean if $valid is set to false, the form is not completely validated and if $valid is set to true the form is completely validated.
  • An empty array $errormsg[] is declared to store error messages. Instead of a single variable we can also use array to store messages.
  • All the required fields are tested to see if they are empty or not using empty() function.
  • The name is checked to see if it is in string format or not using is_string() function.
  • The password along with the empty check is also checked for its length. Our wish is that the password should not have length less than 8.
  • The code given below checks the length of the value in the password field:
  • Here, the length of the password is calculated using strlen() function and stored in the $pl variable.
  • Then the value in $pl is compared against value 8 in if statement. If this condition is true, an error message is stored in array $errormsg[] and $valid variable is set to false.
  • Next the confirm password is also checked for empty test and it is also compared with the password to check its equality.
  • The following code is used to compare the confirm password and the password:
  • If the confirm password is not equal to the password, an error message is stored in the $errormsg[] array.
  • Next the city and state is also validated similar to name for its empty test and to know whether the value entered is a string or not.
  • The mobile number is validated for its empty test and it is also checked that it is numeric and its length is equal to 10.
  • The gender is checked for its empty test.
  • The email id is also checked for its empty test. The email id value is also checked to see if it has the correct format. The code for it is given below:
  • The method filter_var() is used to check the email-id for its correctness.
  • It takes the email-id as a parameter and FILTER_VALIDATE_EMAIL as another parameter to set the format of valid email-id.
  • If the entered email-id format is not valid, an error message is stored in the array $errormsg[].
  • Next the website address is matched with the correct URL format in the preg_match() function. It checks the URL against the regular expression for a valid URL or website address. It takes the regular expression as the first parameter and the user entered URL as the second parameter as shown in the code below:
  • Here, as web address is not compulsory, it is first checked if user has entered URL and then only it is checked for its validity.
  • If the URL is not according to the regular expression, an error message is stored in the array $errormsg.
  • This was all about validation.
  • Let us demonstrate it:
  • The Registration form is shown first in the output:

  • fig 1

  • Now just click on the REGISTER ME button without entering any of the data and see the output.

  • fig 2

  • Now enter information in the form leaving 2 required fields empty or such that errors will arise after clicking REGISTER ME button. The filled form is shown below:

  • fig 3

  • Now just click on REGISTER ME to submit the form and see the output:

  • fig 4

  • In the above fig 4, you can see it clearly that you have three error messages. Form above figure i.e. fig 3 you know here which fields you had entered. But after clicking on REGISTER ME button you can notice that the values which were entered have also been vanished from their place. In this case, the user will again have to fill the whole form. If he has to fill the same information several times till the form is completely validated, he will be fed up of it.
  • To avoid this behavior of the form there is a simple trick.
  • You just have to add the attribute value to all the input tags in html form with the corresponding values entered by user.
  • Just go through the following modified code of the Registration Form.
  • In the above code we have added an attribute value to the input tags.
  • We know that all the values entered by the user are stored in the $_POST superglobal array variable.
  • The value of the particular input field is fetched from the $_POST array variable and put in the field.
  • Let us understand it for the name field. See the code given below:
  • In the input field we have added a value attribute. The value for value attribute is
  • This value is given in PHP tag because the $_POST array variable is used in PHP script.
  • First it is checked whether the value for name is present in $_POST variable using isset() function.
  • If it is present, it is displayed in the text field for name using echo function.
  • The rest of the fields are given the values similarly.
  • Let us understand how to select the same radiobutton that user had selected before submitting the information. The code for radiobutton Male is given below:
  • Here we have value Male in the value attribute for radiobutton Male.
  • We have inserted a PHP tag with some condition and the decision to take if the condition is satisfied. The php tag is given below:
  • Here we check if there is any value stored in the $_POST gender key and if it is Male.
  • If this condition is satisfied, the radiobutton for Male is selected.
  • Similarly the Female is also selected if its condition is satisfied.
  • Only one radiobutton is allowed to be selected because, both radiobuttons have same name i.e. gender given to them.
  • Now just enter the fields in the form leaving some required fields empty and allowing to invoke errors. The output is shown below:

  • fig 5

  • In the above figure we can now see the user entered data still in the fields and the errors for the improper data and required data are shown above.
  • Imagine that after entering the whole information correctly we are redirected to the next page and we get a welcome message along with our particular username.
  • This username can be sent to the next page using the URL of the next page.
  • Let us write the following code in the PHP tag where we have validated all the fields. Write this code below the validation code for url1. The code is given below:
  • Here, we know that if $valid variable has value true, it means the whole form is validated.
  • So when the whole form is validated we are redirected to the next page called welcome.php using header() function.
  • URL is not only used to take you to the next page, but it can also carry some information to the next page.
  • The parameters are passed after the question mark which is given after the url path of the next page.
  • In the above code welcome.php is the path of the next page, then the parameter uname is passed with its value. The value of uname is in the variable $uname.
  • Multiple values can be send through the url by concatenating number of values using ampersand(&) in it as shown below:
  • This value will go to the welcome.php page through get method.
  • This value is retrieved in the welcome.php page using $_GET superglobal array variable as shown in the following code:
  • Here, the username is retrieved using $_GET array variable and stored in variable $unm.
  • It is then displayed using echo statement.

  • The output of the complete correctly filled form is shown below:

  • fig 6

  • Now when we click on the REGISTER ME button, we are redirected to welcome.php page as shown below:

  • fig 7

  • In the above figure you can see the url in the addressbar containing the username of the user.

Thus we now know that URL is not only used to take the user to next page but also to take values with it.

Thus we completed learning how to validate different fields and send values to next page using the URL in this PHP Form URL tutorial.


Please enter your comment!
Please enter your name here