User Registration in PHP

User registration is a very useful feature and it comes almost with every website, social media, and shopping cart or even with a blog. User registration distinguishes information between registered or unregistered users.

So in this tutorial we are going to create user registration form with jquery and PHP validation and save user registration form data into mysql table.

How to create simple login form using Php, Mysql and jQuery

How User Registration in PHP works:

  • Get data from user using input fields (First Name, Last Name, Email, Phone and Password).
  • Perform basic validation using PHP.
  • Check user duplication.
  • Save in Mysql Database.

This is the basic user registration in PHP. Therefore I will focus on the basic functionality of user registration. I will only use some basic jQuery form validation and basic PHP form validation functions. Now let’s start.

php registration form

Create Database:

Create Table:


Database Connection:

config.php

Registration Form HTML:

Index.php

Above is the HTML form with 5 input fields. Form method attribute has POST value which means form data will not be appear in URL (always use post method for secure information like password). Action attribute has $_SERVER['PHP_SELF'] which means form will be submitted on the same page.

$errorMsg and $successMsg variables are used to display error and success messages.

PHP registration form error message     php registration form success message

Style.css

jQuery Validation File

Script.js

In scripts.js there are 5 main functions which will trigger on blur event to verify each field separately. validateEmail function will validate email via regex and validatePhone function will validate phone number in 3 format that are +XX-XXXX-XXXX, +XX.XXXX.XXXX, +XX XXXX XXXX. You can use any phone number regex in var phoneno. I also added blank input validation when the form will submit.

PHP Registration Form after Submit:

trim function is used to remove extra spaces and htmlentities convert characters to html entities. In registration form all fields are required. If any field will come up with blank data then we will through error in $errorMsg. $sql is used to check duplicate email in the database. If $sql query will return 0 record then I will insert data into mysql table otherwise I will through error. $hashPassword is using password_hash function which creates password hash.

How to use PHP password_hash in Registration and Login form

All Code Together:

 

Posted in PHP

Author: Ahsan Zameer

I am a web developer and love to search new stuff on web. Trying different approaches and ideas to make web developing more interesting and enjoyable.