Web Programming Summary
Web Programming Summary
Web Design
Concerned with structure, layout, navigation, visual appeal.
Web pages can be static or dynamic.
Should be simple, well-structured, and easy to find.
Classification of Web Pages:
Typically web pages are classified as static or dynamic:
(a) Static pages don’t change content and layout with every request unless a human (webmaster/programmer)
manually updates the page. A simple HTML page is an example of static content.
(b) Dynamic pages adapt their content and/or appearance depending on enduser’s input/interaction or changes in
the computing environment (user, time, database modifications, etc.) Content can be changed on the client side
(end-user's computer) by using client-side scripting languages (JavaScript, JScript, ActionScript, etc.) to alter DOM
elements (DHTML).
Dynamic content is often compiled on the server utilizing server-side scripting languages (Perl, PHP, ASP, JSP,
ColdFusion, etc.).
Site Types:
(i) Entertainment/Artistic site (rottentomatoes.com, gawkers.com). The characteristics of this site are: Users come:
To have fun
Read thought-provoking issues
To learn
Design may be as or more important than the written content
(ii) Informational site
In this type of site,
It is designed to deliver useful information to thevisitors
Users want information fast and in easy to digest format
Information is more important than the designs
Elements of the site should be arranged in a logical order by importance using size, colours and contrast to make it
clear
Components of Web Design:
HTML/XHTML/XML
CSS
JavaScript (client-side scripting)
PHP/ASP (server-side scripting)
MySQL/PostgreSQL (databases)
Multimedia (Flash, Silverlight)
if (!email.includes("@")) {
alert("Invalid email address.");
return false;
}
return true;
}
</script>
</head>
<body>
<h2>Login</h2>
<form onsubmit="return validateForm()">
<label>Email:</label>
<input type="text" id="email" name="email"><br><br>
<label>Password:</label>
<input type="password" id="password" name="password"><br><br>
Gender:
<label><input type="radio" name="gender" value="Male"> Male</label>
<label><input type="radio" name="gender" value="Female"> Female</label>
<label><input type="radio" name="gender" value="Other"> Other</label><br>
Language:
<select name="language">
<option value="">Select language</option>
<!-- options will be fetched dynamically -->
</select><br>
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$language = $_POST['language'];
$zip = $_POST['zip'];
$about = $_POST['about'];
$sql = "INSERT INTO users (name, email, password, phone, gender, language, zip, about)
VALUES ('$name', '$email', '$password', '$phone', '$gender', '$language', '$zip', '$about')";
if (mysqli_query($conn, $sql)) {
echo "Registration successful";
} else {
echo "Error: " . mysqli_error($conn);
}
?>
CSS Styling
Default File for Styling is a .css file.
Element Selector Example for Header
header {
background-color: blue;
padding: 20px;
}
Horizontally Align Navigation Bar
display: flex; justify-content: center;
Hover Effect for Links
nav a:hover {
color: red;
}
JavaScript
Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a
webpage as a tree of objects that can be manipulated with JavaScript.
Operator that returns remainder is the % (Modulo operator)
Strings in Jscript
Strings can be enclosed in either single or double quotation marks, with different behaviour at read time. Singly
quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as
well as specially interpreting certain character sequences.
Using Jscript to Validate Fields:
<script>
function validateForm() {
let name = document.forms[0]["name"].value;
let email = document.forms[0]["email"].value;
let phone = document.forms[0]["phone"].value;
let zip = document.forms[0]["zip"].value;
let about = document.forms[0]["about"].value;
if (name == "" || email == "" || phone == "" || zip == "" || about == "") {
alert("Please fill out all fields");
return false;
}
if (!email.includes("@")) {
alert("Enter a valid email");
return false;
}
return true;
}
</script>
Attach the script to the form with:
<form onsubmit="return validateForm()">
Linking Web Pages
Use <a> for:
Internal: <a href="page2.html">
External: <a href="https://...">
Email: <a href="mailto:someone@example.com">
JavaScript Animation and Multimedia
Use setInterval() and setTimeout() to animate elements.
Control <audio> and <video> elements using JavaScript.
Example:
<video id="vid" controls><source src="vid.mp4"></video>
<button onclick="document.getElementById('vid').play()">Play</button>
if ($count == 5) {
echo trim($line) . "<br>";
$line = '';
$count = 0;
}
}
if ($line != '') {
echo trim($line); // print any remaining words
}
?>
An Armstrong number is one where the sum of the cube of its digits equals the number.
PHP Armstrong Number Check:
<?php
$number = 371;
$sum = 0;
$temp = $number;
while ($temp != 0) {
$digit = $temp % 10;
$sum += $digit ** 3;
$temp = intdiv($temp, 10);
}
if ($sum == $number) {
echo "$number is an Armstrong number.";
} else {
echo "$number is not an Armstrong number.";
}
?>
HTML Code for the Landing Page (from last year’s PQ)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sweet Bakes Bakery</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
header {
background-color: #f8d7da;
padding: 20px;
text-align: center;
}
nav a {
margin: 0 15px;
text-decoration: none;
color: #333;
font-weight: bold;
}
nav a:hover {
color: red;
}
footer {
background-color: #f1f1f1;
margin-top: 30px;
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<!-- Header Section -->
<header>
<img src="logo.jpg" alt="Company Logo" width="100">
<h1>Sweet Bakes</h1>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Events</a>
<a href="#">Contact Us</a>
</nav>
</header>
</body>
</html>