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>
<?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; } } } ?>
if($_SERVER[“REQUEST_METHOD”]==”POST”)
statement.
$pl=strlen($pwd); if($pl<8) { $errormsg[]='<p class="error">* Password length should not be less than 8.</p>'; $valid=false; }
if($pwd!=$cpwd) { $errormsg[]='<p class="error">* Confirm Password should match the Password.</p>'; $valid=false; }
if(!filter_var($email,FILTER_VALIDATE_EMAIL)) $errormsg[]='<p class="error">* Email-id should be in proper format.</p>'; $valid=false;
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; } }
<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>
<input type="text" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"];?>"/>*<br>
<?php if(isset($_POST["name"])) echo $_POST["name"];?>"
<input type="radio" name="gender" <?php if(isset($_POST["gender"]) && $_POST["gender"]=="Male") echo "checked";?> value="Male"/>Male
<?php if(isset($_POST["gender"]) && $_POST["gender"]=="Male") echo "checked";?>
if($valid==true) header('Location:welcome.php?uname='.$uname);
header(‘Location:welcome.php?var1=value1&var2=value2&var3=value3..);
<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>
It is then displayed using echo statement.
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.