Web Technology - Old Questions

1.Create web form for book search catalog. The form should contain a dropdown defining search type, a text box for search keyword, a radio button for download type true or false, now write PHP script to store data from the form into database table and also retrive the results from stored table in a new page.[10]

10 marks | Asked in 2076(new)

//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>Book Search Catalog</h2>

     <form  method="POST" action="formSubmit.php">

        <div>

            <label for="country">Choose Search Type: </label>

            <select name="searchtype">

                 <option value="history">History</option>

                 <option value="politics">Politics</option>

                 <option value="childrenbook">Children's Book</option>

                <option value="drama">Drama</option>

               <option value="science">Science</option>

            </select>

        </div>

      <div>

            <label for="name">Please enter name of book: </label>

            <input type="text" name="name" id="name">

        </div> 

        <div>

            <label for="download">Download: </label>

            <input type="radio" name="download" value="true">True

            <input type="radio" name="download" value="false">False

        </div>

        <div>

            <input type="submit" name="submit" value="Submit">

        </div>

    </form>

</body>

</html>


//formSubmit.php

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "webtech";

$tablename = "books";


if($_SERVER["REQUEST_METHOD"] == "POST"){

$searchtype = $_POST['searchtype'];

$name = $_POST['name'];

        $download = $_POST['download'];


//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."(searchtype, name, download) VALUES ('$searchtype', '$name', '$download')");    

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

Print '<script>window.location.assign("display.php");</script>'; 

}

?>


//display.php

<?php

    $db = "webtech";

    $conn = mysqli_connect("localhost", "root","") or die(mysql_error());

    mysqli_select_db($conn, $db) or die("Cannot connect to database");

    $query = mysqli_query($conn, "Select * from  books"); 

?>

<html>

<head>

    <title>Displaying Data</title>

</head>

<body>

<table border="1">

<tr>

<th>Search Type</th>

<th>Book Name</th>

<th>Download Type</th>

</tr>

    <?php 

     while($row = mysqli_fetch_array($query)) 

{

    ?>   

<tr>

<th><?php echo $row['searchtype']; ?></th>

<th><?php echo $row['name']; ?></th>

<th><?php echo $row['download']; ?></th>

</tr>

<?php } ?>

</table>

<a href="index.html">Go back to Search New Book</a>

</body>

</html>