You are on page 1of 12

Question One:

Write an HTML page that contains a selection box with a list of 5 countries. When the user
selects a country; its capital should be printed next in the list. Add CSS to customize the
properties of the font of the capital (color, bold and font size). Below is a list of countries and
their capital cities to add and the expected output. [20 marks].
Solution
<html>

<head>
<title>capitals of countries</title>
<style>
p{
color: blue;
font-weight: bold;
font-size: 50;
}
</style>
<script language="javascript">
function capital() {
var cunt = document.forms["P5"].country.value;
var capital = " Please select any country ";
if (cunt == "zimbabwe") {
capital = "Harare";
}
if (cunt == "zambia") {
capital = "Lusaka";
}
if (cunt == "mozambique") {
capital = "Maputo";
}
if (cunt == "botswana") {
capital = "Gaborone";
}
if (cunt == "swaziland") {
capital = "Mbabane";
}

if (cunt == "select") {
capital = "Please select any country";
}

document.getElementById("capt").innerHTML = capital;
}
</script>
</head>

<body>
<form name="P5">
<br/>
<center>
<h1 style="color: red;">Select the country name to find it's capital</h1>
Select a country: <select name="country">
<option value="select">--SELECT--</option>
<option value="zimbabwe">Zimbabwe</option>
<option value="zambia">Zambia</option>
<option value="mozambique">Mozambique</option>
<option value="botswana">Botswana</option>
<option value="swaziland">Swaziland</option>
</select>
</center>
<br/>
<br/>
<!-- <button class="button button1" type="button" onlick="alert('submitted
successfully')">submit</button> -->
<h3>Click on the below Submit Button</h3>

<button onclick="capital()" type="button">Click</button>


<br>

<h3 style="display:inline-block;">
<font color="black" size="4">Capital is :</font>
</h3>
<p id="capt"></p>
<!-- <button type="button" onclick="my_button_click_handler">Click this button</button>
-->
<!-- <script>
function my_button_click_handler()
{
alert('Button Clicked');
}
</script>
<br>
<br>
<font color="green" size="10">Capital is :</font> <p id="capt"></p> -->
</center>
</form>
</body>

</html>

Question Two

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method='GET'>
<h2>Please input your length:</h2>
<h2>Please input your width:</h2>
<input type="text" length="length">
<input type="text" width="width">
<input type="submit" value="Submit Name">
</form>
<?php
//Retrieve name from query string and store to a local variable
$name = $_GET['AREA'];
echo "<h3> Hello $AREA </h3>";
?>
</body>
</html>

Question3:
Develop an HTML program that includes JavaScript script to display a working digital
clock.
<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <title> Digital Clock Using JavaScript </title>

    <link rel="stylesheet" href="styles.css">


</head>

<body>

    <div id="digital-clock"> </div>

    <script src="script.js">
    </script>
    </body>

</html>

function Time() {

      // Creating object of the Date class


     
    var date = new Date();

      // Get current hour


     
    var hour = date.getHours();  // Get current minute
     
    var minute = date.getMinutes();  // Get current second
     
    var second = date.getSeconds();

      // Variable to store AM / PM
     
    var period = "";

      // Assigning AM / PM according to the current hour


     
    if (hour >= 12) {  period = "PM";  } else {  period = "AM";  }

      // Converting the hour in 12-hour format


     
    if (hour == 0) {  hour = 12;  } else {  if (hour > 12) {  hour = hour - 12; 
}  }

      // Updating hour, minute, and second


      // if they are less than 10
     
    hour = update(hour); 
    minute = update(minute); 
    second = update(second);

      // Adding time elements to the div


     
    document.getElementById("digital-clock").innerText = hour + " : " + minute +
" : " + second + " " + period;

      // Set Timer to 1 sec (1000 ms)


     
    setTimeout(Time, 1000);
}

  // Function to update time elements if they are less than 10


  // Append 0 before time elements if they are less than 10
function update(t) { 
    if (t < 10) {  return "0" + t;  } 
    else {  return t;  }
}

Time();
Question 4:
Design a PHP based web application that takes name and age from an HTML page. If the
age is less than 18, it should send a page with “Hello <name>, you are not authorized to
visit the site” message, where <name> should be replaced with the entered name.
Otherwise it should send “Welcome <name> to this site”

<?php
/* ------------ Sessions ------------ */

/*
  Sessions are a way to store information (in variables) to be used across
multiple pages.
  Unlike cookies, sessions are stored on the server.
*/
$age = 18;
 
session_start(); // Must be called before accessing any session data

if (isset($_POST['submit'])) {
  $username = filter_input(
    INPUT_POST,
    'username',
    FILTER_SANITIZE_FULL_SPECIAL_CHARS
  );
  $age = filter_input(
    INPUT_POST,
    'age',
    FILTER_SANITIZE_FULL_SPECIAL_CHARS
  );
 

  if ($username == 'brad' && $age > 17) {


    // Set Session variable
    $_SESSION['username'] = $username;
    // Redirect user to another page
    header('Location: /php-crash/extras/dashboard.php');
  } else {
    echo 'Incorrect username or age';
  }
}
?>

  <form action="<?php echo htmlspecialchars(


    $_SERVER['PHP_SELF']
  ); ?>" method="POST">
    <div>
      <label>Username: </label>
      <input type="text" name="username">
    </div>
    <br>
    <div>
      <label>Age: </label>
      <input type="age" name="age">
    </div>
    <br>
    <input type="submit" name="submit" value="Submit">
  </form>

<?php
session_start();

if (isset($_SESSION['username'])) {
  echo '<h1>Welcome, ' . $_SESSION['username'] . '</h1>';
  echo '<a href="logout.php">Logout</a>';
} else {
  echo '<h1>Welcome, Guest</h1>';
  echo '<a href="/PHP-crash/13_sessions.php">Home</a>'; //this is the dashboard
Php code…
}

<?php
session_start();

// destroy the session


session_destroy();
header('Location: /PHP-crash/13_sessions.php'); // this is the logout page

brad
Sara
Mike
The above are users..

Question5:
Assuming that a database called demo has already been created using MySQL. Write HTML
code for the form below and add php code to achieve the following:
i. Attempt MySQL server connection assuming you are running MySQL server with
default setting (user 'root' with no password)
ii. Check connection
iii. Escape user inputs for security
iv. Attempt to insert query execution
Closing a connection
The below is Html code that will determine the forms of Html.
<!DOCTYPE HTML>
<html>

<head>
    <title>Contact Us</title>
    <link rel="stylesheet" type="text/css" href="style.css">

    <?php include("connectivity.php"); ?>

</head>

<body>
    <div id="contact">
        <h3> Add your records below so that we identify you</h3>
        <form method="POST" action="connectivity.php">
            First Name
            <br>
            <input type="text" name="name">
            <br> Last Name
<input type="text" name="name">
            <br>
            <input type="text" name="email">
            <br> Email Address
            <br>
            <textarea rows="10" cols="50" maxlength="100"
name="message"></textarea>
            <br>
            <input type="submit" value="Add Records">
        </form>
    </div>
</body>
CREATE DATABASE practice;
USE practice;

CREATE TABLE contact


(
contactID INT(9) NOT NULL auto_increment,
contactName VARCHAR(40) NOT NULL,
contactEmail VARCHAR(40) NOT NULL,
addrecords VARCHAR(250) NOT NULL,
PRIMARY KEY(contactID)
);

The above code used to define database tables.


<?php

    $db = new PDO('mysql:host=localhost;dbname=practice;charset=utf8',


                  'root',
                  '',
                  array(PDO::ATTR_EMULATE_PREPARES => false,
                  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

?>
<?php

if (isset($_POST['name'])) {

    $name = $_POST['first name'];


$name = $_POST['first name'];

  $email = $_POST['email'];
    $message =  $_POST['addrecords'];

    $stmt = $db->prepare("INSERT INTO `contact`


(contactName,contactEmail,message)
    VALUES (:name, :email, :add records)");
    $stmt->bindParam(':name', $first name);
$stmt->bindParam(':name', $first name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':addrecords', $addrecords);

    $stmt->execute();

This last one is for connection.

You might also like