Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » HTML Form Validation using PHP and JavaScript
Suryateja Pericherla Categories: Programming. Tags: AJAX, HTML, JavaScript, and PHP. No Comments on HTML Form Validation using PHP and JavaScript
2
(1)

In this article I will show you how to validate a HTML form using PHP and JavaScript. JavaScript performs preliminary checks on the client-side and PHP is used to validate the details entered by the user against the details available in the database on server-side.

 

AJAX has also been used to display the error message within the same webpage (form.html) without any redirections.

 

First let’s see the code of HTML form:

form.html

<!DOCTYPE html>
<html>
	<head>
		<title>Login Form</title>
	</head>
	<body>
		<h2>Login Form</h2>
		<form action="validate.php" onsubmit="return validate()">
			<p>
				<label>Username: </label>
				<input type="text" id="txtuser" name="txtuser" />
			</p>
			<p>
				<label>Password: </label>
				<input type="password" id="txtpass" name="txtpass" />
			</p>
			<p>
				<input type="submit" value="Login" />
				<input type="reset" value="Clear" />
			</p>
		</form>
		<p id="msg"></p>
		<script type="text/javascript" src="script2.js"></script>
	</body>
</html>

 

JavaScript code to perform preliminary validations is as shown below:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


script2.js

function validate()
{
	var uname = document.getElementById("txtuser").value;
	var upass = document.getElementById("txtpass").value;
	xmlHttp = new XMLHttpRequest();
	xmlHttp.onreadystatechange = processResponse;
	var url = "validate.php?uname="+uname+"&upass="+upass;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return false;
}
function processResponse()
{
	if(xmlHttp.readyState==4 && xmlHttp.status==200)
	{
		var para = document.getElementById("msg");
		para.innerHTML = xmlHttp.responseText;
	}
}

 

Finally the PHP code to validate the user details against the data available in the database:

validate.php

<?php
	//$uname = $_GET["txtuser"];
	//$upass = $_GET["txtpass"];
	$uname = $_GET["uname"];
	$upass = $_GET["upass"];
	$flag = false;
	$con=mysqli_connect("localhost","root","123456","test");
	$result = mysqli_query($con, "select * from users"); 
	while($x = mysqli_fetch_array($result))
	{
		if($uname == $x["uname"] && $upass == $x["upass"])
			$flag = true;
	}

	if($flag)
	  echo "Valid user!";
	else
	  echo "Invalid username or password!";
?>

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory