User registration with server-side validation using PHP
In this post, we will learn user registration with server side validation. First create a HTML form then validate this form by using PHP functions.
Registration form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<form method="post"> <div class="card card-6"> <div class="card-heading"> <h2 class="title">User Registration</h2> </div> <div class="card-body"> <p style="color:red;" align="center"><?php if(isset($error)) { echo $error; }?> </p> <div class="form-row"> <div class="name">Full name</div> <div class="value"> <input type="text" name="fullname" class="input--style-6" value="<?php if(isset($fname)){echo $fname;} ?>" <?php if(isset($code) && $code == 1){ echo "autofocus"; } ?> /> </div> </div> <div class="form-row"> <div class="name">Mobile Number</div> <div class="value"> <div class="input-group"> <input type="text" name="mobilenumber" class="input--style-6" value="<?php if(isset($mnumber)){echo $mnumber;} ?>" <?php if(isset($code) && $code == 2){ echo "autofocus"; } ?> /> </div> </div> </div> <div class="form-row"> <div class="name">Email id</div> <div class="value"> <div class="input-group"> <input type="text" name="emailid" class="input--style-6" value="<?php if(isset($emailid)){echo $emailid;} ?>" <?php if(isset($code) && $code ==3){ echo "autofocus"; } ?> /> </div> </div> </div> <div class="form-row"> <div class="name">Password</div> <div class="value"> <div class="input-group"> <input type="password" name="password" class="input--style-6" value="<?php if(isset($password)){echo $password;} ?>" <?php if(isset($code) && $code ==4){ echo "autofocus"; } ?> /> </div> </div> </div> <div class="form-row"> <div class="name">Confirm Password</div> <div class="value"> <div class="input-group"> <input type="text" name="confirmpassword" class="input--style-6" value="<?php if(isset($cpassword)){echo $cpassword;} ?>" <?php if(isset($code) && $code ==5){ echo "autofocus"; } ?> /> </div> </div> </div> </div> <div class="card-footer"> <button class="btn btn--radius-2 btn--blue-2" type="submit" name="submit">Submit</button> </div> </div> </form> |
In this form, we have the following fields :
- Fullname
- Mobile Number
- Email id
- Password
- Confirm Password
In this form, we are going to fix the following things:
- Empty value
- Numeric value
- Valid email id
- value matching
- Validate existing email and mobile number
PHP Script
For validating the form and data insertion in the database PHP code will be as follow and put this code at the top the page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?php include_once('config.php'); if(isset($_POST['submit'])) { $fname=$_POST['fullname']; $mnumber=$_POST['mobilenumber']; $emailid=$_POST['emailid']; $password=$_POST['password']; $cpassword=$_POST['confirmpassword']; //Serverside Validation if(empty($fname)) { $error = "Enter your fullname !"; $code = 1; } if(empty($fname)) { $error = "Enter your fullname !"; $code = 1; } else if(empty($mnumber)) { $error = "Enter your mobile number !"; $code = 2; } else if(!is_numeric($mnumber)) { $error = "Mobile number must be numeric only!"; $code = 2; } else if(strlen($mnumber)!=10) { $error = "Mobile nuber should be 10 digit only!"; $code = 2; } else if(empty($emailid)) { $error = "Enter your email !"; $code = 3; } else if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $emailid)) { $error = "Enter valid email id !"; $code = 3; } else if(empty($password)) { $error = "Enter your password"; $code = 4; } else if(strlen($password) < 6 ) { $error = "Password must be 6 characters long !"; $code = 4; } else if(empty($cpassword)) { $error = "Enter your confirm password"; $code = 5; } else if(strlen($cpassword) < 6 ) { $error = "Confirm Password must be 6 characters long !"; $code = 5; } else if($cpassword!=$password) { $error = "Password and Confirm Password doesnot match"; $code = 5; } else{ //Checking emailid and mobile number if already registered $ret=mysqli_query($con, "select id from tblregistration where EmailId='$emailid' || MobileNumber='$mnumber'"); $result=mysqli_fetch_array($ret); if($result>0){ echo "<script>alert('This email already associated with another account');</script>"; } else{ $query=mysqli_query($con,"insert into tblregistration(FullName,MobileNumber,EmailId,Password) values('$fname','$mnumber','$emailid','$password')"); if($query){ echo "<script>alert('Data submitted')</script>"; echo "<script>window.location.href='registration.php';</script>"; } else { echo "<script>alert('Something went wrong. Please try again.')</script>"; echo "<script>window.location.href='registration.php';</script>"; } } } } ?> |