Web Technologies - Old Questions
1. Develop a simple website that asks the users to register using username, password, and email address. Store these information’s in a database. Use client side script to validate the user input during registration.
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Registration</title>
</head>
<body>
<h2>Client Side Form Validation</h2>
<form name="signupForm" 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>
<label for="email">Please enter your email: </label>
<input type="email" name="email" id="email">
</div>
<div>
<input type="submit" name="signup" value="Signup">
</div>
</form>
<script>
function validateForm() {
if (document.signupForm.username.value == "") {
alert("Please provide your usernamename!");
document.signupForm.username.focus();
return false;
}
else if (document.signupForm.password.value == "") {
alert("Please provide your password!");
document.signupForm.password.focus();
return false;
}
else if (document.signupForm.password.value.length < 8) {
alert('Password must be at least 8 digit long');
document.signupForm.password.focus();
return false;
}
else if (document.signupForm.email.value == "") {
alert("Please provide your Email!");
document.signupForm.email.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'];
$email = $_POST['email'];
//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, email) VALUES ('$username','$password', '$email')");
Print '<script>alert("Congrats! Your Submission is Successfully Registered!");</script>';
?>