Registration page in PHP


In this registration page we use javascript for the validation of a blank form, numeric validation, email validation. The connection between php scripts and the mysql database is done in the config.php file. Here we use a registration.php page where the user can enter their details. After clicking the register button welcome.php page will appear with the user name.

There are three ways in which we can collect values in a form. They are:

  • $_GET variable, which is used to collect values in a form with, get method
  • $_POST variable is used to collect values in a form with post method
  • $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods. Here we use $_POST variables to retrieve information from forms.

Table creation

Server: localhost Database: onlinetest
-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Dec 05, 2011 at 04:16 AM
-- Server version: 5.0.45
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `onlinetest`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE `user`
(
    `name` varchar(255) NOT NULL,
    `address` varchar(255) NOT NULL,
    `area` varchar(255) NOT NULL,
    `city` varchar(255) NOT NULL,
    `pin` int(11) NOT NULL,
    `state` varchar(255) NOT NULL,
    `country` varchar(255) NOT NULL,
    `mobile` int(20) NOT NULL,
    `email` varchar(255) NOT NULL,
    `user` varchar(255) NOT NULL,
    `password` varchar(255) NOT NULL
)
ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`name`, `address`, `area`, `city`, `pin`, `state`, `country`, `mobile`, `email`, `user`, `password`) VALUES
('Satyapriya', 'Survey of India', 'R.R Laboratory', 'Bhubaneswar', 751013, 'Orissa', 'India', 9938866290, '[email protected]', 'Satyapriya', 'nayak');

config.php

<?php
$sDbHost = 'localhost';
$sDbName = 'onlinetest';
$sDbUser = 'root';
$sDbPwd = '';
$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());
?>

registration.php

<?php
session_start();
include("config.php");
if($_POST["Submit"]=="Register")
{
    $name=$_POST["text_name"];
    $address=$_POST["text_address"];
    $area=$_POST["text_area"];
    $city=$_POST["text_city"];
    $pin=$_POST["text_pin"];
    $state=$_POST["text_state"];
    $country=$_POST["text_country"];
    $mobile=$_POST["text_mobile"];
    $email=$_POST["text_email"];
    $user=$_POST["text_user"];
    $password=$_POST["text_pass"];
    $_SESSION["username"]=$_POST["text_name"];

    //echo $_SESSION["username"];
    //exit;
    $str="insert into user(name,address,area,city,pin,state,country,mobile,email,user,password)value
    ('".$name."','".$address."','".$area."','".$city."','".$pin."','".$state."','".$country."','".$mobile."','".$email."',           
    '".$user."','".$password."')";

    //echo $str;
    //exit;
    mysql_query($str);
    echo "<script>location.href='welcome.php'</script>";}?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Registration</title>
    </head>
    <script language="javascript" type="text/javascript">
    function validate()
    {
    if(document.getElementById("text_name").value=="")
    {
        alert("Please Enter Your Name");
        document.getElementById("text_name").focus();
        return false;
    }

    if(!(isNaN(document.registration.text_name.value)))
    {
        alert("Name has character only!");
        return false;
    }
 
    if(document.getElementById("text_address").value=="")
    {
        alert("Please Enter Your Address");
        document.getElementById("text_address").focus();
        return false;
    }
 
    if(!(isNaN(document.registration.text_address.value)))
    {
        alert("Address has character only!");
        return false;
    }
 
    if(document.getElementById("text_area").value=="")
    {
        alert("Please Enter Your Area");
        document.getElementById("text_area").focus();
        return false;
    }
 
    if(!(isNaN(document.registration.text_area.value)))
    {
        alert("Area has character only!");
        return false;
    }
 
    if(document.getElementById("text_city").value=="")
    {
        alert("Please Enter Your City");
        document.getElementById("text_city").focus();
        return false;
    }
 
    if(!(isNaN(document.registration.text_city.value)))
    {
        alert("City has character only!");
        return false;
    }
 
    if(document.getElementById("text_pin").value=="")
    {
        alert("Please Enter Your Pin");
        document.getElementById("text_pin").focus();
        return false;
    }
 
    if((isNaN(document.registration.text_pin.value)))
    {
        alert("Pin has numeric only!");
        return false;
    } 

    if(document.getElementById("text_state").value=="")
    {
        alert("Please Enter Your State");
        document.getElementById("text_state").focus();
        return false;
    }

    if(!(isNaN(document.registration.text_state.value)))
    {
        alert("State has character only!");
        return false;
    }

    if(document.getElementById("text_country").value=="")
    {
        alert("Please Enter Your Country");
        document.getElementById("text_country").focus();
        return false;
    }

    if(!(isNaN(document.registration.text_country.value)))
    {
        alert("Country has character only!");
        return false;
    }

    if(document.getElementById("text_mobile").value=="")
    {
        alert("Please Enter Your Mobile Number");
        document.getElementById("text_mobile").focus();
        return false;
    }

    if((isNaN(document.registration.text_mobile.value)))
    {
        alert("Mobile has numeric only!");
        return false;
    }
    var emailPat=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    var emailid=document.getElementById("text_email").value;
    var matchArray = emailid.match(emailPat);

    if (matchArray == null)
    {
        alert("Your email address seems incorrect. Please try again.");
        document.getElementById("text_email").focus();
        return false;
    }

    if(document.getElementById("text_user").value=="")
    {
        alert("Please Enter User Name");
        document.getElementById("text_user").focus();
        return false;
    }

    if(document.getElementById("text_pass").value=="")
    {
        alert("Please Enter Your Password");
        document.getElementById("text_pass").focus();
        return false;
    }

    if(document.getElementById("text_repass").value=="")
    {
        alert("Please ReEnter Your Password");
        document.getElementById("text_repass").focus();
        return false;
    }
    if(document.getElementById("text_repass").value!="")
    {
          if(document.getElementById("text_repass").value != document.getElementById("text_pass").value)
          {
               alert("Confirm Password doesnot match!");
               document.getElementById("text_repass").focus();
               return false;
          }
    }
    return true;
}
</script>
    <body>
        <form name="registration" action="registration.php" method="post" onsubmit="return validate();">
        <center>
    <table border="1">
      <tr>
            <td colspan="2">User Registration Form </td>
     </tr>
       <tr>
        <td width="179">Name<em>*</em></td>
    <td><label>
      <input name="text_name" type="text" id="text_name" />
    </label></td>
  </tr>
  <tr>
    <td width="179">Address  <em>*</em></td>
    <td><label>
      <input name="text_address" type="text" id="text_address" />
    </label></td>
  </tr>
  <tr>
    <td width="179">Area<em>*</em></td>
    <td><label>
      <input name="text_area" type="text" id="text_area" />
    </label></td>
  </tr>
  <tr>
    <td width="179">City  <em>*</em></td>
    <td><label>
      <input name="text_city" type="text" id="text_city" />
    </label></td>
  </tr>
  <tr>
    <td width="179">Pin<em>*</em></td>
    <td><label>
      <input name="text_pin" type="text" id="text_pin" />
    </label></td>
  </tr>
  <tr>
    <td width="179">State<em>*</em></td>
    <td><label>
      <input name="text_state" type="text" id="text_state" />
    </label></td>
  </tr>
  <tr>
    <td width="179">Country<em>*</em></td>
    <td><label>
      <input name="text_country" type="text" id="text_country" />
    </label></td>
  </tr>
  <tr>
    <td width="179">Phone/Mobile<em>*</em></td>
    <td><label>
      <input name="text_mobile" type="text" id="text_mobile" />
    </label></td>
  </tr>
            <tr>
    <td width="179">Email address <em>*</em></td>
    <td><label>
      <input name="text_email" type="text" id="text_email" />
    </label></td>
  </tr>
  <tr>
    <td>User Name<em>*</em></td>
    <td><label>
      <input name="text_user" type="text" id="text_user" />
    </label></td>
  </tr>
  <tr>
    <td>Password <em>*</em></td>
    <td><label>
      <input name="text_pass" type="password" id="text_pass" />
    </label></td>
  </tr>
  <tr>
    <td>Confirm Password <em>*</em></td>
    <td><label>
      <input name="text_repass" type="password" id="text_repass" />
    </label></td>
  </tr>
  <tr align="center">
    <td colspan="2"><label>
      <input type="submit" name="Submit" value="Register" />
    </label></td>
    </tr>
</table>
</center>
</form>
</body>
</html> 

welcome.php
 
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Welcome</title>
    </head>
    <body>
          <form action="welcome.php" method="get" name="welcome">
              <?php echo '<b>Welcome:::</b>'.$_SESSION["username"]; ?>
          </form>
    </body>
</html>

Running the application

Run the WampServer then write the below line in the Url

http://localhost/Registration/

registrationPhp.gif

Up Next
    Ebook Download
    View all
    Learn
    View all