Web Technologies - Old Questions
1. Develop a website that checks validity of a user during the log-in process. Assume that the login data (username and password) are already stored in a database. Use client-side script to validate the user input during login process.
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
</head>
<body>
<h2>Client Side Form Validation</h2>
<form name="loginForm" onsubmit="return validateForm();" method="POST" action="./formSubmit.php">
<div>
<label for="username">Please enter your username: </label>
<input type="text" name="username" id="username">
</div>
<div>
<label for="password">Please enter your password: </label>
<input type="password" name="password" id="password">
</div>
<div>
<input type="submit" name="login" value="login">
</div>
</form>
<script>
function validateForm() {
if (document.loginForm.username.value == "") {
alert("Please provide your usernamename!");
document.loginForm.username.focus();
return false;
}
else if (document.loginForm.password.value == "") {
alert("Please provide your password!");
document.loginForm.password.focus();
return false;
}
else if (document.loginForm.password.value.length < 8) {
alert('Password must be at least 8 digit long');
document.loginForm.password.focus();
return false;
}
return true;
}
</script>
</body>
</html>
//formSubmit.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webtech";
$tablename = "users";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = $_POST['username'];
$password = $_POST['password'];
//connect to database
$conn = mysqli_connect($servername, $username, $password) or die(mysql_error()); //Connect to server
mysqli_select_db($conn, $dbname) or die("Cannot connect to database"); //Connect to database
// Insert the values into database
mysqli_query($conn, "INSERT INTO ".$tablename."(username, password) VALUES ('$username','$password')");
Print '<script>alert("Congrats! Your Submission is Successfully Registered!");</script>';
?>