Wednesday, 31 December 2014

HTML , PHP - Registration Form

In this post i will show you how to create a basic registration page and store those values in the database and also do some error checking like empty values are not allowed.

Here i will use xampp as a server and create a database there with 4 columns username , password, gender and name. These are the values we will store in our database name sample and table name users.

Now open a file in any text editor and place the following code :



$servername = "";
$username = "codingcorner";
$password = "yourPassword";

$nameErr = $unameErr = $genderErr = $pwdErr = "";
$name = $uname = $gender = $comment = $pwd = "";

  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);

  if (empty($_POST["uname"])) {
    $unameErr = "Username is required";
  } else {
    $uname = test_input($_POST["uname"]);

  if (empty($_POST["pwd"])) {
    $pwdErr= "Password is required";
  } else {
    $pwd = test_input($_POST["pwd"]);

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);

try {
    $conn = new PDO("mysql:host=$servername;dbname=sample", $username, $password);
    // set the PDO error mode to exception
     $sql = "INSERT INTO users (username, password , gender , name)
    VALUES ('$uname', '$pwd','$gender','$name')";
    // use exec() because no results are returned
    echo "Welcome !";
catch(PDOException $e)
    echo "Connection failed: " . $e->getMessage();

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;


<h2>A Simple Registration Form</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name">
   <span class="error">* <?php echo $nameErr;?></span>
   Username: <input type="text" name="uname">
   <span class="error">* <?php echo $unameErr;?></span>
   Password: <input type="password" name="pwd">
   <span class="error">*<?php echo $pwdErr;?></span>
   Comment: <textarea name="comment" rows="5" cols="40"></textarea>
   <input type="radio" name="gender" value="female">Female
   <input type="radio" name="gender" value="male">Male
   <span class="error">* <?php echo $genderErr;?></span>
   <input type="submit" name="submit" value="Submit"> 

echo "<h2>Your Details:</h2>";
echo $name;
echo "<br>";
echo $uname;
echo "<br>";
echo $pwd;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;

Now save this file as registration-tuts.php or any other name you wish in C:\xampp\htdocs or any path where your xampp is installed .
 Start Apache and Mysql in your xampp control panel.

 It looks something like this

Now open any browser and give the url as http://localhost/registration-tuts.php 
now the page we just created will open


When the * mark (required fields ) are not filled it shows an error telling this field is required .

When all the fields  are filled .

Now we will check the database we created in the beginning for the details entered here.

In the browser go to localhost/phpmyadmin and then in the right side select your database and table , then it will show you the details you entered in the form we created.

That's it ! We are done with our registration form.