Web Technology - Old Questions
3. Prepare a form that should contain text box, selection list and radio button. Write PHP script to store data from the form into database using database connection and appropriate query.
//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>
<form method="POST" action="formSubmit.php">
<div>
<label for="name">Please enter your name: </label>
<input type="text" name="name" id="name">
</div>
<div>
<label for="email">Please enter email: </label>
<input type="text" name="email" id="email">
</div>
<div>
<label for="country">Choose your Country: </label>
<select name="country">
<option value="nepal">Nepal</option>
<option value="china">China</option>
<option value="india">India</option>
<option value="usa">USA</option>
</select>
</div>
<div>
<label for="gender">Gender: </label>
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
</div>
<div>
<input type="submit" name="signup" value="Signup">
</div>
</form>
</body>
</html>
//formSubmit.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "webtech";
$tablename = "users";
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = $_POST['name'];
$email = $_POST['email'];
$country = $_POST['country'];
$gender = $_POST['gender'];
//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."(name, email, country, gender) VALUES ('$name', '$email', '$country', '$gender')");
Print '<script>alert("Congrats! Your Submission is Successfully Registered!");</script>';
?>