Creating And Processing A Web Form Using HTML 5 & PHP

0
40
Web Form Using HTML 5

This blog includes the end-to-end procedure of creating and processing a web form using HTML5 and PHP. Our example in this tutorial includes a registration web form which every website includes for registration of new users of website. The parameters included for the registration form include:

1. Username
2. E-mail
3. Password

Pre-requisites:

The primary pre-requisites include understanding PHP with SQL connectivity. It is assumed that user has primary setup of PHP web application installed in their system. We are using HTML5 for achieving validations of the form.
The procedure of creating and processing a web form using HTML5 and PHP are mentioned below:

First step which is mandatory for managing the forms starts with creation of database. As our website will primarily accept all the users in registration module, we will be considering the database name as “users” and table name with the same.

The data dictionary of “users” table is displayed below:

users-table

The columns included in “users” table are id (Primary key), username, email and passwords.
The database connectivity with PHP web application is achieved through following script:

Now we will focus on the registration form creation.
The look and feel of the registration form and login form are visible below:

register

login-form

The code for creating the registration form is mentioned below:

In the above code, we have included the validations in every form input which is in-built in HTML5 and have included some third-party files and libraries including “.css” files for styling the view part.

The following code is placed before using the view part which means .html file with sole reason of achieving connectivity with database. In this section, we start a session using PHP and initialize the database connectivity with the help of file “dbconnect.php”. After initializing the connectivity, the parameters of username, email and password which are passed through HTML form is inserted in the users table. The insert query is same SQL standard query of “INSERT”.

if (isset($_SESSION[‘user’]) != “”) {
header(“Location: index.php”);
}
include_once ‘dbconnect.php’;

if (isset($_POST[‘signup’])) {

$uname = trim($_POST[‘uname’]); // get posted data and remove whitespace
$email = trim($_POST[’email’]);
$upass = trim($_POST[‘pass’]);

// hash password with SHA256;
$password = hash(‘sha256’, $upass);

// check email exist or not
$stmt = $conn->prepare(“SELECT email FROM users WHERE email=?”);
$stmt->bind_param(“s”, $email);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

$count = $result->num_rows;

if ($count == 0) { // if email is not found add user

$stmts = $conn->prepare(“INSERT INTO users(username,email,password) VALUES(?, ?, ?)”);
$stmts->bind_param(“sss”, $uname, $email, $password);
$res = $stmts->execute();//get result
$stmts->close();

$user_id = mysqli_insert_id($conn);
if ($user_id > 0) {
$_SESSION[‘user’] = $user_id; // set session and redirect to index page
if (isset($_SESSION[‘user’])) {
print_r($_SESSION);
header(“Location: index.php”);
exit;
}

} else {
$errTyp = “danger”;
$errMSG = “Something went wrong, try again”;
}

} else {
$errTyp = “warning”;
$errMSG = “Email is already used”;
}

}
?>

The styling of files are designed in two files namely: style.css and various plugins. The code snippet included in assets/style.css is mentioned below:

Once user the required parameters in registration form with proper validation check, we can click on the submit button and have a look on the records been inserted in the SQL database.

Registration-image

The successful insertion of input entries through form in database table is mentioned below:

users-entry

It is observed that the passwords are stored in SHA-1 hash format to maintain security. This security feature will not allow intruders to access passwords easily. In login form design, these passwords are decrypted and verified with user entry. If the user entry matches with password, the user can access the website else if authentication fails, the proper validation message is displayed.

The login form of website is designed as below:

The access of login form with username and password is demonstrated below:

login-form

The user dashboard after successful login is displayed below:

Hello demo eduonix com

The password matches with username inserted in the records of database table, the user has access to home page. If incorrect password is inserted, wrong validation message is shown up which says, “Bad password”.

login-fail

Conclusion:

In this tutorial, we have demonstrated creating a PHP contact form using AJAX, jQuery and MySQL. The primary focus is to include HTML5 tags for perfect view design along with database structure and queries inserted. Always keep a look on the action used which can be “Get” or “Post” method. As passwords are inserted, the recommended action to be used is “Post” method to maintain security.

LEAVE A REPLY

Please enter your comment!
Please enter your name here