Web Technology - Old Questions

Question Answer Details

3.  Prepare a form that should contain text box for name and email, selection list for country. Write a server side script to store data from the form into database. The script should contain connection to database and appropriate query into the database.

10 marks
Asked in 2076

Answer

AI Generated Answer

AI is thinking...

Official Answer

//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>

            <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'];


//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) VALUES ('$name', '$email', '$country')"); 

Print '<script>alert("Congrats! Your Submission is Successfully Registered!");</script>'; 

?>