PHP Code For Registration Form

PHP Code Snippets No Comments

PHP Code For Registration Form
Before you start writing PHP Code For Registration Form, you need to start with writing small HTML code for creating form elements, and styling them so that they look good.

Step 1 – Add HTML:

Create an HTML file, say index.html, and use an <form> element to process the input. Then add inputs (with a matching label) for each field:

<form action="submitform.php">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" id="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" id="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw-repeat" id="psw-repeat" required>
    <hr>
    <button type="submit" class="registerbtn">Register</button>
  </div>
</form>
Step 2 – Add CSS

Add the below-mentioned code in the above HTML file so that the form element looks elegant.

* {box-sizing: border-box}

/* Add padding to containers */
.container {
  padding: 16px;
}

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* Set a style for the submit/register button */
.registerbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

.registerbtn:hover {
  opacity:1;
}

/* Add a blue text color to links */
a {
  color: dodgerblue;
}
Step 3 – submitform.php file code which inserts the data into the database.
<?php
//First of all, we'll have to include the database_connection.php file so that we can use the functions which we have defined in that file.
include 'database_connection.php';

// We'll connect with the database using StartDatabaseConnection() function. We are getting the result of calling this function in connectdb variable. If the connection to database becomes successful, this variable value will be true else it will be false.

//establish connection
$connectdb = StartDatabaseConnection();

//Code start for inserting the data in the database.
if(isset($_POST['submit']))
{    
     $name = $_POST['name'];
     $email = $_POST['email'];
     $mobile = $_POST['mobile'];
     $sql = "INSERT INTO users (name,email,mobile)
     VALUES ('$name','$email','$mobile')";
     if (mysqli_query($connectdb, $sql)) {
        echo "New record has been inserted in the database successfully!";
     } else {
        echo "Error: " . $sql . ":-" . mysqli_error($connectdb);
     }
}
else {
        echo "<br />"."Some issue.";
     } 

//Code ends for inserting the data into the database.

//close connection
CloseDatabaseConnection($connectdb);
?>

Click the below link to check the demo. Download link to download the files used in this tutorial is also available on this page.
Demo

If you’re a freelancer, you can contact us here to send your resume for freelancing projects.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments