PHP Form URL

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:
  • <?php
    
    $valid=false;
    if($_SERVER["REQUEST_METHOD"]=="POST")
    {
    	$name=$_POST["name"];
    	$uname=$_POST["uname"];
    	$pwd=$_POST["pwd"];
    	$cpwd=$_POST["cpwd"];
    	$city=$_POST["city"];
    	$state=$_POST["state"];
    	$mob=$_POST["mob"];
    	$gender=$_POST["gender"];
    	$email=$_POST["email"];
    	$url=$_POST["url1"];
    	$errormsg=array();
    	if(empty($name))
    	{
    		$errormsg[]='<p class="error">* Name cannot be blank.</p>';
    		$valid=false;
    	}
    	else
    	{
    		if(is_string($name))
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* Name should be in string format.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($uname))
    	{
    		$errormsg[]='<p class="error">* UserName cannot be blank.</p>';
    		$valid=false;
    	}
    	
    	if(empty($pwd))
    	{
    			$errormsg[]='<p class="error">* Password cannot be blank.</p>';
    			$valid=false;
    	}
    	else
    	{
    		$pl=strlen($pwd);
    		if($pl<8)
    		{
    			$errormsg[]='<p class="error">* Password length should not be less than 8.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($cpwd))
    	{
    		$errormsg[]='<p class="error">* Confirm Password field cannot be blank.</p>';
    			$valid=false;
    	}
    	
    	if($pwd!=$cpwd)
    	{
    		$errormsg[]='<p class="error">* Confirm Password should match the Password.</p>';
    			$valid=false;
    	}
    	if(empty($city))
    	{
    		$errormsg[]='<p class="error">* City is compulsory.</p>';
    		$valid=false;
    	}
    	else
    	{
    		if(is_string($city))
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* City should be in string format.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($state))
    	{
    		$errormsg[]='<p class="error">* State is compulsory.</p>';
    		$valid=false;
    	}
    	else
    	{
    		if(is_string($state))
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* State should be in string format.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($mob))
    	{
    		$errormsg[]='<p class="error">* Mobile number is compulsory.</p>';
    		$valid=false;
    	}
    	else
    	{
    		$len=strlen($mob);
    		
    		if(is_numeric($mob) && $len==10)
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* Mobile number should be in number format with 10 digits in it.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($gender))
    	{
    		$errormsg[]='<p class="error">* Please select your gender.</p>';
    		$valid=false;
    	}
    	
    	if(empty($email))
    	{
    		$errormsg[]='<p class="error">* Email-id is compulsory.</p>';
    		$valid=false;
    	}
    	
    	else
    	{
    		if(!filter_var($email,FILTER_VALIDATE_EMAIL))
    			$errormsg[]='<p class="error">* Email-id should be in proper format.</p>';
    			$valid=false;
    		
    	}
    	if(!empty($url))
    {
    	if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url1))
    	{
    		$errormsg[]='<p class="error">* website address should be in proper format.</p>';
    		$valid=false;
    	}
    }
    }
    ?>
    
    <html>
    <head>
    <title>Form Validation</title>
    <style>
    		h2{
    			margin-left:100px;
    		}
    		label{
    			margin-left:10px;
    			display:inline-block;
    			width:180px;
    		}
    		
    		#decor{
    			font-weight:bold;
    			color:#1C478E;
    		}
    		.shift{
    			padding:5px;
    			margin-left:155px;
    			color:#1C478E;
    			font-weight:bold;
    		}
    		.error{
    			color:red;
    		}
    		.style{
    			border-width:300px;
    			border:#054dbc solid 4px;
    			border-radius:10px;
    		}
    </style>
    </head>
    <body id="decor">
    <h2>Registration Form</h2>
    <?php
    	if(!empty($errormsg))
    		foreach($errormsg as $r)
    			echo $r;
    ?>
    <form class="style" name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    
    <p class="error">* required fields</p>
    
    <label for="name">Full Name:</label>
    <input type="text" name="name" />*<br>
    <label for="name">UserName:</label>
    <input type="text" name="uname" />*<br>
    <label for="name">Password:</label>
    <input type="password" name="pwd" />*<br>
    <label for="cpwd">Confirm Password:</label>
    <input type="password" name="cpwd" />*<br>
    <label for="city">City:</label>
    <input type="text" name="city"/>*<br>
    <label for="state">State:</label>
    <input type="text" name="state"/>*<br>
    <label for="mob">Mobile Number:</label>
    <input type="text" name="mob"/>*<br>
    <label for="gender">Gender:</label>
    <input type="radio" name="gender" value="Male"/>Male
    <input type="radio" name="gender" value="Female"/>Female   *<br>
    <label for="email">Email-id:</label>
    <input type="text" name="email"/>*<br>
    <label for="url1">Website address:</label>
    <input type="text" name="url1"/>
    <input class="shift" type="submit" value="REGISTER ME"/>
    </form>
    </body>
    </html>
    
  • 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:
  • <?php
    
    $valid=false;
    if($_SERVER["REQUEST_METHOD"]=="POST")
    {
    	$name=$_POST["name"];
    	$uname=$_POST["uname"];
    	$pwd=$_POST["pwd"];
    	$cpwd=$_POST["cpwd"];
    	$city=$_POST["city"];
    	$state=$_POST["state"];
    	$mob=$_POST["mob"];
    	$gender=$_POST["gender"];
    	$email=$_POST["email"];
    	$url=$_POST["url1"];
    	$errormsg=array();
    	if(empty($name))
    	{
    		$errormsg[]='<p class="error">* Name cannot be blank.</p>';
    		$valid=false;
    	}
    	else
    	{
    		if(is_string($name))
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* Name should be in string format.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($uname))
    	{
    		$errormsg[]='<p class="error">* UserName cannot be blank.</p>';
    		$valid=false;
    	}
    	
    	if(empty($pwd))
    	{
    			$errormsg[]='<p class="error">* Password cannot be blank.</p>';
    			$valid=false;
    	}
    	else
    	{
    		$pl=strlen($pwd);
    		if($pl<8)
    		{
    			$errormsg[]='<p class="error">* Password length should not be less than 8.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($cpwd))
    	{
    		$errormsg[]='<p class="error">* Confirm Password field cannot be blank.</p>';
    			$valid=false;
    	}
    	
    	if($pwd!=$cpwd)
    	{
    		$errormsg[]='<p class="error">* Confirm Password should match the Password.</p>';
    			$valid=false;
    	}
    	if(empty($city))
    	{
    		$errormsg[]='<p class="error">* City is compulsory.</p>';
    		$valid=false;
    	}
    	else
    	{
    		if(is_string($city))
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* City should be in string format.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($state))
    	{
    		$errormsg[]='<p class="error">* State is compulsory.</p>';
    		$valid=false;
    	}
    	else
    	{
    		if(is_string($state))
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* State should be in string format.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($mob))
    	{
    		$errormsg[]='<p class="error">* Mobile number is compulsory.</p>';
    		$valid=false;
    	}
    	else
    	{
    		$len=strlen($mob);
    		
    		if(is_numeric($mob) && $len==10)
    			$valid=true;
    		else
    		{
    			$errormsg[]='<p class="error">* Mobile number should be in number format with 10 digits in it.</p>';
    			$valid=false;
    		}
    	}
    	
    	if(empty($gender))
    	{
    		$errormsg[]='<p class="error">* Please select your gender.</p>';
    		$valid=false;
    	}
    	
    	if(empty($email))
    	{
    		$errormsg[]='<p class="error">* Email-id is compulsory.</p>';
    		$valid=false;
    	}
    	
    	else
    	{
    		if(!filter_var($email,FILTER_VALIDATE_EMAIL))
    			$errormsg[]='<p class="error">* Email-id should be in proper format.</p>';
    			$valid=false;
    		
    	}
    	
    	if(!empty($url))
    {
    
    	if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url1))
    	{
    		$errormsg[]='<p class="error">* website address should be in proper format.</p>';
    		$valid=false;
    	}
    }
    	
    }
    
    ?>
    
  • It is checked that if the information filled in the form is submitted using
    if($_SERVER[“REQUEST_METHOD”]==”POST”)

    statement.

  • 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:
  • $pl=strlen($pwd);
    		if($pl<8)
    		{
    			$errormsg[]='<p class="error">* Password length should not be less than 8.</p>';
    			$valid=false;
    		}
    
  • 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($pwd!=$cpwd)
    	{
    		$errormsg[]='<p class="error">* Confirm Password should match the Password.</p>';
    			$valid=false;
    	}
    
  • 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:
  • if(!filter_var($email,FILTER_VALIDATE_EMAIL))
    	$errormsg[]='<p class="error">* Email-id should be in proper format.</p>';
    	$valid=false;
    
  • 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:
  • if(!empty($url1))
    {
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url1))
    	{
    		$errormsg[]='<p class="error">* website address should be in proper format.</p>';
    		$valid=false;
    	}
    }
    
  • 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:
  • form
    fig 1

  • Now just click on the REGISTER ME button without entering any of the data and see the output.
  • all_errors_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:
  • partially_filled_form
    fig 3

  • Now just click on REGISTER ME to submit the form and see the output:
  • output_when_partially_filled_form_is_submitted
    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.
  • <form class="style" name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <p class="error">*required fields</p>
    <label for="name">Full Name:</label>
    <input type="text" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"];?>"/>*<br>
    <label for="uname">UserName:</label>
    <input type="text" name="uname" value="<?php if(isset($_POST["uname"])) echo $_POST["uname"];?>"/>*<br>
    <label type="pwd">Password:</label>
    <input type="password" name="pwd" />*<br>
    <label for="cpwd">Confirm Password:</label>
    <input type="password" name="cpwd" />*<br>
    <label for="city">City:</label>
    <input type="text" name="city" value="<?php if(isset($_POST["city"])) echo $_POST["city"];?>"/>*<br>
    <label for="state">State:</label>
    <input type="text" name="state" value="<?php if(isset($_POST["state"])) echo $_POST["state"];?>"/>*<br>
    <label for="mob">Mobile Number:</label>
    <input type="text" name="mob" value="<?php if(isset($_POST["mob"])) echo $_POST["mob"];?>"/>*<br>
    <label for="gender">Gender:</label>
    <input type="radio" name="gender" <?php if(isset($_POST["gender"]) && $_POST["gender"]=="Male") echo "checked";?> value="Male"/>Male
    <input type="radio" name="gender" <?php if(isset($_POST["gender"]) && $_POST["gender"]=="Female") echo "checked";?> value="Female"/>Female *<br>
    <label for="email">Email-ID:</label>
    <input type="text" name="email" value="<?php if(isset($_POST["email"])) echo $_POST["email"];?>"/>*<br>
    <label for="url1">Website Address:</label>
    <input type="text" name="url1" value="<?php if(isset($_POST["url1"])) echo $_POST["url1"]?>"/><br>
    <input  class="shift" type="submit" value="REGISTER ME"/>
    </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:
  • <input type="text" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"];?>"/>*<br>
  • In the input field we have added a value attribute. The value for value attribute is
  •  <?php if(isset($_POST["name"])) echo $_POST["name"];?>"
  • 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:
  • <input type="radio" name="gender" <?php if(isset($_POST["gender"]) && $_POST["gender"]=="Male") echo "checked";?> value="Male"/>Male
  • 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:
  • <?php if(isset($_POST["gender"]) && $_POST["gender"]=="Male") echo "checked";?>
  • 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:
  • values_seen_after_partially_filled_form
    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:
  • if($valid==true)
    	header('Location:welcome.php?uname='.$uname);
    
  • 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:
  • header(‘Location:welcome.php?var1=value1&var2=value2&var3=value3..);
  • 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:
  • <html>
    <head>
    <title>Welcome</title>
    <style>
    		#decor{
    			font-weight:bold;
    			color:#1C478E;
    		}
    </style>
    </head>
    <body id="decor">
    <?php
    $unm=$_GET["uname"];
    echo "<h1>".$unm." Welcome to our shopping mart</h1>";
    ?>
    </body>
    </html>
    
  • 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:
  • completely_filled_form
    fig 6

  • Now when we click on the REGISTER ME button, we are redirected to welcome.php page as shown below:
  • welcome_page
    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.

Previous articlePHP String Functions
Next articlePHP Form Validation

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -