1. HTML Form (pay_slip_form.
html): Captures employee details and submits them to
<!DOCTYPE html>
<html>
<head>
<title>Employee Pay Slip</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid black; padding: 10px; text-align: center; }
th { background-color: #f2f2f2; }
.container { max-width: 500px; margin: auto; }
</style>
</head>
<body>
<div class="container">
<h2>Employee Pay Slip Form</h2>
<form action="pay_slip.php" method="post">
<label>Name:</label>
<input type="text" name="name" required><br><br>
<label>Working Hours:</label>
<input type="number" name="hours" required><br><br>
<label>Hourly Rate:</label>
<input type="number" name="rate" required><br><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
2. PHP Script (pay_slip.php):
<?php
session_start();
if (!isset($_SESSION['employees'])) {
$_SESSION['employees'] = [];
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$hours = (int)$_POST['hours'];
$rate = (float)$_POST['rate'];
// Calculate gross salary
if ($hours <= 50) {
$gross_salary = $hours * $rate;
} else {
$gross_salary = (50 * $rate) + (($hours - 50) * $rate * 2);
}
// Store employee details
$_SESSION['employees'][] = [
'name' => $name,
'hours' => $hours,
'rate' => $rate,
'gross_salary' => $gross_salary
];
}
// Calculate total salary expense
$total_salary = array_sum(array_column($_SESSION['employees'], 'gross_salary'));
?>
<!DOCTYPE html>
<html>
<head>
<title>Pay Slip Details</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { border: 1px solid black; padding: 10px; text-align: center; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h2>Employee Salary Details</h2>
<table>
<tr>
<th>Name</th>
<th>Working Hours</th>
<th>Hourly Rate</th>
<th>Gross Salary</th>
</tr>
<?php foreach ($_SESSION['employees'] as $employee): ?>
<tr>
<td><?php echo htmlspecialchars($employee['name']); ?></td>
<td><?php echo $employee['hours']; ?></td>
<td><?php echo $employee['rate']; ?></td>
<td><?php echo number_format($employee['gross_salary'], 2); ?></td>
</tr>
<?php endforeach; ?>
</table>
<h3>Total Salary Paid: <?php echo number_format($total_salary, 2); ?></h3>
</body>
</html>
- Processes submitted data.
- Calculates the gross salary based on the given conditions.
- Stores the data using PHP sessions to keep a cumulative record of employees.
- Displays all employee details in a table.
- Computes and displays the total salary expense.