WTL Assignment 8
WTL Assignment 8
Assignment No.8
Aim:
Design a shopping application form with following fields [itemID, itemName,
itemQuantity,
Write a PHP script to add and display the items
Theory:
PHP stands for Hypertext Preprocessor. PHP is a very popular and widely-used open source
server-side scripting language to write dynamically generated web pages. PHP was originally
created by Rasmus Lerdorf in 1994. It was initially known as Personal Home Page.
PHP scripts are executed on the server and the result is sent to the web browser as plain
HTML.
PHP can be integrated with the number of popular databases, including MySQL,
PostgreSQL,
Oracle, Microsoft SQL Server, Sybase, and so on
The values assigned to a PHP variable may be of different data types including simple string
and numeric types to more complex data types like arrays and objects.
PHP supports a total eight primitive data types: Integer, Floating point number or Float,
String, Booleans, Array, Object, resource and NULL. These data types are used to construct
variables. Now let's discuss each one of them in detail.
Arrays are complex variables that allow us to store more than one value or a group of values
under a single variable name.
Types of Arrays in PHP
There are three types of arrays that you can create. These are:
Indexed array/ Numeric array — An array with a numeric key.
Associative array — An array where each key has its own specific value.
Multidimensional array — An array containing one or more arrays within itself.
Code:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f2f2f2;
padding: 50px;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: #fff;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
color: #2c3e50;
}
.form-group {
margin-bottom: 20px;
}
.form-group input {
width: 100%;
padding: 12px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.form-group input:focus {
border-color: #3498db;
background-color: #fff;
}
button {
padding: 12px 25px;
font-size: 16px;
background-color: #2ecc71;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s;
}
button:hover {
background-color: #27ae60;
}
table {
width: 100%;
margin-top: 30px;
border-collapse: collapse;
text-align: left;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 12px;
}
th {
background-color: #3498db;
color: white;
}
td {
background-color: #f9f9f9;
}
.alert {
padding: 10px;
background-color: #e67e22;
color: white;
text-align: center;
border-radius: 5px;
display: none;
}
</style>
</head>
<body>
<div class="container">
<h2>Shopping Cart</h2>
<form method="POST">
<div class="form-group">
<input type="text" name="itemID" placeholder="Enter Item ID" required>
</div>
<div class="form-group">
<input type="text" name="itemName" placeholder="Enter Item Name" required>
</div>
<div class="form-group">
<input type="number" name="itemQuantity" placeholder="Enter Item Quantity"
required>
</div>
<button type="submit" name="addItem">Add Item</button>
</form>
<h3>Items in Cart</h3>
<table>
<thead>
<tr>
<th>Item ID</th>
<th>Item Name</th>
<th>Item Quantity</th>
</tr>
</thead>
<tbody>
<?php
session_start();
if (!isset($_SESSION['items'])) {
$_SESSION['items'] = [];
}
<script>
$(document).ready(function() {
$("form").on("submit", function() {
$("input[type='text'], input[type='number']").val('');
});
});
</script>
</body>
</html>
Output:
Conclusion: