Home > DeveloperSection > Articles > Registration in PHP

Registration in PHP


PHP PHP 
Ratings:
0 Comment(s)
 249  View(s)
Rate this:

Registration in PHP


Following are the steps to create registration in PHP:


·         Create registration form

·         Connection with the database

·         Validation using Javascript

Step 1: Create registration.php form. In this a form is made which has method “post” and action attribute which will submit your processing page or as I do it in current page. Following is a code for registration.php:

Code Implementation:

<style>

.user

{

  text-align: center;

  width:35%;

  background-color: lightgrey;

  margin-top:20px;

  height:70%

  line-height:10px;

}

.user1

{

     margin-top:3px;

    

           width:220px;

           height:30px;

            margin: 8px 0;

}       

 

 

input[type=submit]

{

   

    background-color: #4CAF50;

    color: white;

    padding: 14px 20px;

    margin: 8px 0;

    border: none;

    border-radius: 4px;

    cursor: pointer;

}

 

          input[type=submit]:hover

{

    background-color: lightblue;

input[type=text],input[type=number],input[type=password]

 {

   

    padding: 12px 20px;

    margin: 8px 0;

    display: inline-block;

    border: 1px solid #ccc;

    border-radius: 6px;

    box-sizing: border-box;

}

.date

{

    padding: 5px 10px;

    margin: 8px 0;

    display: inline-block;

    border: 1px solid #ccc;

    border-radius: 6px;

    box-sizing: border-box;

}       

table

{

      text-align: center;

            margin-left:40px;

           

            }

   

</style>

<center>

<div class="user">

<form method="post" name="myForm" onsubmit="return validate()">

<h2><u>REGISTRATION FORM</u></h2>

<table>

<tr><td>Username:</td><td colspan="3"><input type="text" name="uname"  class="user1"></td></br>

<tr><td>Password:</td><td colspan="3"><input type="password" name="pass"  class="user1"></td></br>

<tr><td>Name   :</td><td colspan="3"><input type="text" name="name" class="user1"></td></br>

<tr><td>D.O.B:</td><td><select name="year" class="date" id='year'>

<option value='Select' selected="selected">Year</option>

<?php

for($i=date('Y');$i>=1980;$i--)

{

     echo("<option>$i</option>");

}       

?>

</select></td>

 

<td><select name="month" class="date" id='month'>

<option value='Select' selected="selected">Month</option>

<?php

$array=array(1=>'Jan', 2=>'Feb', 3=>'Mar’,  4=>'April', 5=>'May', 6=>'June', 7=>'Jul', 8=>'Aug', 9=>'Sep', 10=>'Oct', 11=>'Nov', 12=>'Dec');

     foreach($array as $k=>$v)

{

     echo("<option value='$k'>$v</option>");

}       

?>

</select></td>

<td><select name="day" id='day' class="date">

<option value='Select' selected="selected">Day</option>

<?php

for($i=1;$i<=31;$i++)

{

     echo("<option>$i</option>");

}       

?>

</select></br></td></tr>

<tr><td>Phone:</td><td colspan="3"><input type="number" name="phone"  class="user1"></br></td></tr>

<tr>

<td></td>

<td></td>

<td><input type="submit" name="submit" value="Submit" class="user3"></br></td></tr>

</table>

</form>

</div>

 

Note:  onsubmit="return validate()" is used for validation using javascript and onsubmit is the event which will be call when user click on submit button. We will discuss about this later in this section. In the above code date(‘Y’) function will give the current year. Rest code is the styling code which you can give your own as you 

 

Step 2: Connection with the database

After creating a form now we have to create a connection with the database server where data will be stored. Following is a code for creating a connection with the database (connect.php):

Code Implementation

 

<?php

 

$con = mysqli_connect("192.168.1.51","username","password","project");

// creating table

$sql = "CREATE TABLE user_login (     //user_login is the name of the table which should be given

username INT(6) PRIMARY KEY,

password MEDIUMTEXT

name VARCHAR(30) NOT NULL,

dob DATE;

phone VARCHAR(13);

 

if ($con->query($sql) === TRUE) {

    echo "Table user_login created successfully";

} else {

    echo "Error creating table: " . $con->error;

}

 

?>

 

As you can see from the above code, username is the name of the user and password given by the user. Project is the name of your database. From the following code your database project and your table in project database is created.

Step 3: Validation using Javascript

Now we are having a form which is submitting the fields which are entered by the user. Now we will apply some validation on these fields. It will check whether all the fields are entered or not. Function validate is a function which will validate all the inputs in the fields entered by the user and this function validate() is called in first part where form is made onsubmit=”return validate()”.Following is a code for validation using Javascript:

<?php

 

if(isset($_POST["submit"]))    // following code will only run if the user have submitted the form

{  

               $uname=$_POST["uname"];

           $pass=$_POST["pass"];

           $pass=md5($pass);

           $name=$_POST["name"];

           $dob=$_POST["year"]."-". $_POST["month"]."-". $_POST["day"];

           

           $phone=$_POST["phone"];

           require_once("connect.php");

         

           $check="SELECT COUNT(*) FROM user_login WHERE username = '$uname'";

          $Result= mysqli_query($con, $check);

                           if (mysqli_num_rows($Result)>=1)

             {

                             echo "Username Already in Exists<br/>";

              }

                       else{

                   $con->query("insert into user_login values('$uname', '$pass',' $name', '$dob', $phone)");

             }

          }

         

         

?>

 

<script>

function validate()

{

              var uname=document.forms["myForm"]["uname"].value;

           var uname=document.forms["myForm"]["pass"].value;

           var uname=document.forms["myForm"]["name"].value;

           var uname=document.forms["myForm"]["phone"].value;

         

           

           

   if(uname == null || uname == "" )    // If user have not entered any field an alert box will be generated.

  

           {

               alert("Field must not be left blank.");

                    return false;

           }

           

            if (document.getElementById('year').value == 'Select')

 

        {

           alert("Field must not be left blank.");

                      return false;

       }

            else if (document.getElementById('month').value == 'Select')

 

        {

           alert("Field must not be left blank.");

                     return false;

       }

           else

           if (document.getElementById('day').value == 'Select')

 

        {

           alert("Field must not be left blank.");

                      return false;

       }

           

}       

</script>

 

Now, from the above code we can see when the user have not entered any field in the database it will show an alert box Field must not be left blank. As shown below how alert box is given when user have not entered the username:


As from the above code when user have not entered any field and submitted the form then an alert message is shown which displays “Field must not be left blank”.

Now if user entered the same name which already exists in our database then a message is shown “Username Already in Exists. Below is the merge code for registration.php page where you can copy and run the program easily:

 

<style>

.user

{

  text-align: center;

  width:35%;

  background-color: lightgrey;

  margin-top:20px;

  height:70%

  line-height:10px;

}

.user1

{

     margin-top:3px;

    

           width:220px;

           height:30px;

            margin: 8px 0;

}       

 

 

input[type=submit]

{

   

    background-color: #4CAF50;

    color: white;

    padding: 14px 20px;

    margin: 8px 0;

    border: none;

    border-radius: 4px;

    cursor: pointer;

}

 

          input[type=submit]:hover

{

    background-color: lightblue;

input[type=text],input[type=number],input[type=password]

 {

   

    padding: 12px 20px;

    margin: 8px 0;

    display: inline-block;

    border: 1px solid #ccc;

    border-radius: 6px;

    box-sizing: border-box;

}

.date

{

    padding: 5px 10px;

    margin: 8px 0;

    display: inline-block;

    border: 1px solid #ccc;

    border-radius: 6px;

    box-sizing: border-box;

}       

table

{

      text-align: center;

            margin-left:40px;

           

            }

   

</style>

<center>

<div class="user">

<form method="post" name="myForm" onsubmit="return validate()">

<h2><u>REGISTRATION FORM</u></h2>

<table>

<tr><td>Username:</td><td colspan="3"><input type="text" name="uname"  class="user1"></td></br>

<tr><td>Password:</td><td colspan="3"><input type="password" name="pass"  class="user1"></td></br>

<tr><td>Name   :</td><td colspan="3"><input type="text" name="name" class="user1"></td></br>

<tr><td>D.O.B:</td><td><select name="year" class="date" id='year'>

<option value='Select' selected="selected">Year</option>

<?php

for($i=date('Y');$i>=1980;$i--)

{

     echo("<option>$i</option>");

}       

?>

</select></td>

 

<td><select name="month" class="date" id='month'>

<option value='Select' selected="selected">Month</option>

<?php

$array=array(1=>'Jan', 2=>'Feb', 3=>'Mar’,  4=>'April', 5=>'May', 6=>'June', 7=>'Jul', 8=>'Aug', 9=>'Sep', 10=>'Oct', 11=>'Nov', 12=>'Dec');

     foreach($array as $k=>$v)

{

     echo("<option value='$k'>$v</option>");

}       

?>

</select></td>

<td><select name="day" id='day' class="date">

<option value='Select' selected="selected">Day</option>

<?php

for($i=1;$i<=31;$i++)

{

     echo("<option>$i</option>");

}       

?>

</select></br></td></tr>

<tr><td>Phone:</td><td colspan="3"><input type="number" name="phone"  class="user1"></br></td></tr>

<tr>

<td></td>

<td></td>

<td><input type="submit" name="submit" value="Submit" class="user3"></br></td></tr>

</table>

</form>

</div>

<?php

 

if(isset($_POST["submit"]))

{  

 

      

     $uname=$_POST["uname"];

           $pass=$_POST["pass"];

           $pass=md5($pass);

           $name=$_POST["name"];

           $dob=$_POST["year"]."-".$_POST["month"]."-".$_POST["day"];

           

           $phone=$_POST["phone"];

           require_once("connect.php");

         

           $check="SELECT COUNT(*) FROM user_login WHERE username = '$uname'";

          $Result= mysqli_query($con,$check);

if (mysqli_num_rows($Result)>=1)

{

    echo "Username Already in Exists<br/>";

           else{

          $con->query("insert into user_login values('$uname','$pass','$name','$dob',$phone)");

           }

          }

         

         

?>

 

<script>

function validate()

{

     var uname=document.forms["myForm"]["uname"].value;

           var uname=document.forms["myForm"]["pass"].value;

           var uname=document.forms["myForm"]["name"].value;

           var uname=document.forms["myForm"]["phone"].value;

         

                  

   if(uname == null || uname == "" )

 {

alert("Feild must not be left blank.");

                    return false;

           }

           

            if (document.getElementById('year').value == 'Select')

 

        {

           alert("Please select Year");

                      return false;

       }

            else if (document.getElementById('month').value == 'Select')

 

        {

           alert("Please select Month");

                     return false;

       }

           else

           if (document.getElementById('day').value == 'Select')

 

        {

           alert("Please select Day");

                      return false;

       }

           

}       

</script>


In next article, we will discuss how to Login and logout in PHP.


Don't want to miss updates? Please click the below button!

Follow MindStick