Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Assignment_3

Uploaded by

Muhammad Junaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment_3

Uploaded by

Muhammad Junaid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

National University of Modern

Languages
(Faisalabad Campus)

Subject Web

Semester BSSE 5th

Assignment 03

Date 5 Dec 2024

Name Muhammad Junaid Akbar

Roll No FSD-FL-114
Question # 1:
Create a form fields such as name, price and description. Use placeholders to
guide the required data format. Implement an empty field check on form
submission. Upon clicking, transfer the required enter data to a separate page
named “Check.php” On the receiving page validate the existence of data and
apply regular expression checks. Additionally, specify the method employed for
sending data from this form.
Form Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Submission</title>
</head>
<body>
<form action="Check.php" method="POST" onsubmit="return validateForm()">
<input type="text" name="name" placeholder="Enter name" id="name"><br>
<input type="text" name="price" placeholder="Enter price (e.g., 100.50)"
id="price"><br>
<textarea name="description" placeholder="Enter description"
id="description"></textarea><br>
<button type="submit">Submit</button>
</form>

<script>
function validateForm() {
const name = document.getElementById("name").value.trim();
const price = document.getElementById("price").value.trim();
const description =
document.getElementById("description").value.trim();

if (!name || !price || !description) {


alert("All fields are required!");
return false;
}
return true;
}
</script>
</body>
</html>
Check.php:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];

if (empty($name) || empty($price) || empty($description)) {


echo "All fields must be filled!";
exit;
}

// Validate with regular expressions


if (!preg_match("/^[a-zA-Z\s]+$/", $name)) {
echo "Invalid name format!";
} elseif (!preg_match("/^\d+(\.\d{2})?$/", $price)) {
echo "Invalid price format!";
} elseif (strlen($description) < 10) {
echo "Description must be at least 10 characters long.";
} else {
echo "Data received and validated successfully!";
// Proceed with further processing if needed
}
}
?>

Output:
Question # 2:
Create a database of ABC in phpMyAdmin. Consider three tables User,
product, and Order. You can add table attributes accordingly. But it is
necessary to add primary and foreign keys to each table. Write a code for
webpage to connect with your database. Secondly, includes your database
connectivity file to select records from the order table and display them on
a web page table for view.
Database Structure:
CREATE DATABASE ABC;

USE ABC;

CREATE TABLE User (


user_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
);

CREATE TABLE Product (


product_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL
);

CREATE TABLE `Order` (


order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
product_id INT NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES User(user_id),
FOREIGN KEY (product_id) REFERENCES Product(product_id)
);

Database Connection File:


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ABC";

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

Display Records:
<?php
include 'db_connect.php';

$sql = "SELECT o.order_id, u.name AS user_name, p.name AS product_name,


o.order_date
FROM `Order` o
JOIN User u ON o.user_id = u.user_id
JOIN Product p ON o.product_id = p.product_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>User Name</th>
<th>Product Name</th>
<th>Order Date</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['order_id']}</td>
<td>{$row['user_name']}</td>
<td>{$row['product_name']}</td>
<td>{$row['order_date']}</td>
</tr>";
}
echo "</table>";
} else {
echo "No orders found!";
}
$conn->close();
?>
Output:
Question # 3:
What do you know about associative array in PHP/JavaScript to store and
manipulate data. Using an example of an associative array to demonstrate
how to access, modify and loop through its elements. Also explain the
practical scenario where associative array would be particularly useful in
PHP/JavaScript programming.
$student = [
"name" => "John Doe",
"age" => 20,
"major" => "Computer Science"
];

// Access
echo "Name: " . $student["name"] . "<br>";

// Modify
$student["age"] = 21;

// Loop
foreach ($student as $key => $value) {
echo "$key: $value<br>";
}
const student = {
name: "John Doe",
age: 21,
major: "Computer Science"
};

// Access
console.log("Name:", student.name);

// Modify
student.age = 26;

// Loop
for (const key in student) {
console.log(`${key}: ${student[key]}`);
}

Output:
Question # 4:
What do you know about the following given below and explain
with the help of an examples?
 Cookies Management
 File Handling
 Ajax
 Pagination
1. Cookies Management:
Output:

2. File Handling:


Output:

3. AJAX:

Output:
 Output:

You might also like