All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Validation In PHP Registration Form

Last Updated : Mar 11, 2024

Validation In PHP Registration Form

In this tutorial we will show you the solution of validation in PHP registration form and today we are going to understand how to create a Registration form with validation.

Registration form is also known as Sign-up form. And it this we are going to apply validation. So, let us see how to apply validation on Registration form given below.

Step By Step Guide On Validation In PHP Registration Form :-

Here, below first we have created a database with name index and then inside this there is a table having name rl_form.

The validation on this form are applied using jQuery, so be sure that you jQuery file must be linked using script tag with this form. Let us see the below example for better understanding.

<?php
    $connect=mysqli_connect("localhost","root","","index") or die("Connectoin Failed");
    if(!empty($_REQUEST['button']))
    {
        $user=$_REQUEST['username'];
        $email=$_REQUEST['email'];
        $pass=$_REQUEST['password'];
        $cpass=$_REQUEST['cpassword'];
        $filename= $_FILES['file']['name'];
        $filepath=$_FILES['file'] ['tmp_name'];
        $imagename= explode(".",$filename);
        $ext=$imagename[1];
        $query = "show table status like 'rl_form'";
        $result = mysqli_query($connect,$query);
        $row=mysqli_fetch_assoc($result);
        $id=$row['Auto_increment'];
        $newfilename= $id.".".$ext;
        if($user=='' || $email== '' || $pass== '' || $cpass== '' || $filename == '')
        {
            echo "<script> alert('Please fill all fields.......')</script>";
        }
        else{
            $query="select * from rl_form where email= '$email'";
            $result=mysqli_query($connect,$query);
            $count = mysqli_num_rows($result);
            if($count>0)
            {
                echo "<script> alert(' Email id alreadty exist')</script>";
            }
            else
            {
                if($pass != $cpass)
                {
                    echo "<script> alert(' Please enter same password')</script>";
                }
                else
                {
                    $query=" insert into rl_form( user, email, pass, cpass, image) values('$user', '$email', '$pass' , '$cpass' , '$newfilename')";
                    if(mysqli_query($connect, $query))
                    {
                        move_uploaded_file($filepath,"uploadimages/".$newfilename);
                        echo " <script> alert('Record inserted successfully')
                        window.location.href= '../project/index.php';
                        </script>";
                    }
                    else{
                        echo " Error found ! come back after one year";
                    }
                }
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
    <script src="js/jquery.js"></script>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="main">
        <div class="inner">
            <div class="welcome">
                Registration Form
            </div>
            <div class="registration_options">
                <form action="" method="post" enctype="multipart/form-data">
                    <div class="rleft_body">
                        <label for="">
                            Enter your name here
                        </label>
                        <br>
                        <label for="">
                            Enter your email here
                        </label>
                        <br>
                        <label for="">
                            Enter your password here
                        </label>
                        <br>
                        <label for="">
                            Enter password again here
                        </label>
                        <label for="">
                            Upload your image here
                        </label>
                    </div>
                    <div class="rright_body">
                        <input type="text" name="username" id="">
                        <br><br>
                        <input type="email" name="email" id="">
                        <br><br>
                        <input type="password" name="password" id="">
                        <br><br>
                        <input type="text" name="cpassword" id="">
                        <br><br>
                        <input type="file" name="file" id="" accept="images/*">
                    </div>
                    <div class="registration_submit">
                        <input type="submit" value="Save" id="save" name="button">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, first of all as you see that there is a registration form with fields name, email, password, and confirm password.
  6. And we use our database with jQuery to apply validation like - Is email id provided by user is valid or not, Is password is greater than 8 digits or not, Is password and confirm password both are same, Is email id provided by user already exists or not, Is all the fields given in form are filled or not, etc.
  7. Here, we apply some validation on registration form. We can also add or remove some validations using php. We hope that you understand the above codes easily.
  8. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to create a registration form with validations in php.

I hope this tutorial on validation in PHP registration form helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪