HTML/PHP. Hello, I am having difficulty with this program. It is suppose to calc
ID: 3670627 • Letter: H
Question
HTML/PHP. Hello, I am having difficulty with this program. It is suppose to calculate an employee's net pay. The user is suppose to enter hourly pay rate, number of hours worked in a week, and the number of dependents they claim. If the employee works more than 45 hours in a week, overtime is calculated at 1.5 times the regular hourly rate. Taxes are then deducted from the gross pay as follows: no dependents = tax rate of 25%, 1 to 3 dependents = tax rate of 20%, 4 to 6 dependents = tax rate of 10%, and more than 6 dependents = tax rate of 5%. Below are the index.php file and the index_process.php file (where these calculations take place) I think I have the logic right just not the placement of the if else loop. Any help would be appreciated.
INDEX.PHP
<?php
$hourlyRate = filter_input(INPUT_POST, 'hourlyRate');
$hoursWorked = filter_input(INPUT_POST, 'hoursWorked');
$dependents = filter_input(INPUT_POST, 'dependents');
?>
<html lang="en">
<head> <!-- open of head -->
<meta charset="utf-8">
<title> Employee Net Pay </title>
<link rel="stylesheet" href="style.css"> <!-- connects to style sheet -->
</head>
<body> <!-- beginning of the body section -->
<?php if (!empty($error_message)) { ?>
<p class="error"><?php echo htmlspecialchars($error_message); ?></p>
<?php } ?>
<header>
<h1> Employee Net <br>Pay </h1>
</header>
<!--paragraph is outputted to end user-->
<p><br> This page will prompt __ to calculate an employee's net pay.<br>
Please enter:<br></p>
<ul>
<li>Employee's hourly rate of pay;</li>
<li>Number of hours worked that week;</li>
<li>Number of dependents employee has.</li>
</ul><br>
<h2> Please enter: </h2>
<!--creates link to index_process.php and creates field to enter power level-->
<form action="index_process.php" method="post">
<div id= "form">
<label>Employee's hourly rate of pay:      </label>
<input type="text" name="hourlyRate" value="30" size="15"> dollars per hour<br><br>
<label>Number of hours worked that week:   </label>
<input type="text" name="hoursWorked" value="50" size="15"> hours<br><br>
<label>Number of dependents employee has:  </label>
<input type="text" name="dependents" value="3" size="15"> dependents<br><br>
</div>
<div id="button">
<label> </label>              
      <input type="submit" value="submit"/><br>
</div>
</form>
<footer>
<br><br><br><br>
</footer>
</body>
</html>
INDEX_PROCESSING.PHP
<?php
$hourlyRate = $_POST['hourlyRate'];
$hoursWorked = $_POST['hoursWorked'];
$dependents = $_POST['dependents'];
$hoursWorked_escaped = htmlspecialchars($hoursWorked);
$hourlyRate_escaped = htmlspecialchars($hourlyRate);
$dependents_escaped = htmlspecialchars($dependents);
?>
<html lang="en">
<head> <!-- open of head -->
<meta charset="utf-8">
<title> Employee Net Pay </title> <!-- title of webpage -->
<link rel="stylesheet" href="style.css"> <!-- connects to style sheet -->
</head>
<body> <!-- beginning of the body section -->
<header>
<h1> Employee Net <br>Pay </h1>
</header>
<!-- paragraph and list on webpage -->
<p><br> This page calculates an employer's net pay. </p><br>
<h2>You entered:</h2><br>
<!-- php to calcuate the employee net pay -->
<?php
{
{
if ($dependents = 0)
{
$taxRate = 25;
$calculateTaxRate = 0.25;
}
else if ($dependents >= 1 && $dependents <= 3)
{
$taxRate = 20;
$calculateTaxRate = 0.20;
}
else if ($dependents >= 4 && $dependents <= 6)
{
$taxRate = 10;
$calculateTaxRate = 0.10;
}
else if ($dependents > 6)
{
$taxRate = 5;
$calculateTaxRate = 0.05;
}
echo "Employee's hourly rate of pay = $". "<b>". $hourlyRate. "</b>". " per hour". "<br> ";
echo "Number of hours worked that week = ". "<b>". $hoursWorked. "</b>". " hours". "<br> ";
echo "Number of dependents employee has = ". "<b>". $dependents. "</b>". " dependents". "<br> ";
$overtimeHours = ($hoursWorked - 45);
echo "Number of overtime hours = ". "<b>". $hoursWorked. " - 45 = ". $overtimeHours. "</b>". " hours". "<br> ";
$firstFourtyFive = ($hourlyRate * 45);
echo "First 45 hours pay = ". "<b>". "$". $hourlyRate. " x 45 = $". $firstFourtyFive. "</b>". "<br> ";
$overtime = ($hourlyRate * $overtimeHours * 1.5);
echo "Next ". $overtimeHours. " hours pay (overtime) = ". "<b>". "$". $hourlyRate. " x ". $overtimeHours. " x 1.5 = $". $overtime. "</b>". "<br> ";
$grossPay = ($firstFourtyFive + $overtime);
echo "Gross pay = ". "<b>". "$". $firstFourtyFive. " + ". "$". $overtime. " = $". $grossPay. "</b>". "<br> ";
echo "Tax rate for ". "<b>". $dependents. "</b>". " dependents = ". "<b>". $taxRate. "</b>". "<br> ".
$tax = ($grossPay * $calculateTaxRate);
$taxDeduction = ($tax + $grossPay);
echo "Tax deduction = ". "<b>". $grossPay. " x ". $taxRate. "% = $". $taxDeduction. "</b>". "<br> ";
$netPay = ($grossPay - $taxDeduction);
echo "Net pay = ". "<b>". "$". $grossPay. " - $". $taxDeduction. " = $". $netPay. "</b><br> ";
echo "This employee earned: <b>". $netPay. "</b> this week.". "<br> ";
}
}
?>
<br><br>
<!-- below is to display the back to previous page button -->
  <a href="index.php">back to first page</a>
<br><br>
</body>
</html>
Explanation / Answer
NOTE : Run below script. It is giving correct output
index.php
<?php
$hourlyRate = filter_input(INPUT_POST, 'hourlyRate');
$hoursWorked = filter_input(INPUT_POST, 'hoursWorked');
$dependents = filter_input(INPUT_POST, 'dependents');
?>
<html lang="en">
<head> <!-- open of head -->
<meta charset="utf-8">
<title> Employee Net Pay </title>
<link rel="stylesheet" href="style.css"> <!-- connects to style sheet -->
</head>
<body> <!-- beginning of the body section -->
<?php if (!empty($error_message)) { ?>
<p class="error"><?php echo htmlspecialchars($error_message); ?></p>
<?php } ?>
<header>
<h1> Employee Net <br>Pay </h1>
</header>
<!--paragraph is outputted to end user-->
<p><br> This page will prompt __ to calculate an employee's net pay.<br>
Please enter:<br></p>
<ul>
<li>Employee's hourly rate of pay;</li>
<li>Number of hours worked that week;</li>
<li>Number of dependents employee has.</li>
</ul><br>
<h2> Please enter: </h2>
<!--creates link to index_process.php and creates field to enter power level-->
<form action="index_process.php" method="post">
<div id= "form">
<label>Employee's hourly rate of pay:      </label>
<input type="text" name="hourlyRate" value="30" size="15"> dollars per hour<br><br>
<label>Number of hours worked that week:   </label>
<input type="text" name="hoursWorked" value="50" size="15"> hours<br><br>
<label>Number of dependents employee has:  </label>
<input type="text" name="dependents" value="3" size="15"> dependents<br><br>
</div>
<div id="button">
<label> </label>              
      <input type="submit" value="submit"/><br>
</div>
</form>
<footer>
<br><br><br><br>
</footer>
</body>
</html>
INDEX_PROCESSING.php
<?php
$hourlyRate = $_POST['hourlyRate'];
$hoursWorked = $_POST['hoursWorked'];
$dependents = $_POST['dependents'];
$hoursWorked_escaped = htmlspecialchars($hoursWorked);
$hourlyRate_escaped = htmlspecialchars($hourlyRate);
$dependents_escaped = htmlspecialchars($dependents);
?>
<html lang="en">
<head> <!-- open of head -->
<meta charset="utf-8">
<title> Employee Net Pay </title> <!-- title of webpage -->
<link rel="stylesheet" href="style.css"> <!-- connects to style sheet -->
</head>
<body> <!-- beginning of the body section -->
<header>
<h1> Employee Net <br>Pay </h1>
</header>
<!-- paragraph and list on webpage -->
<p><br> This page calculates an employer's net pay. </p><br>
<h2>You entered:</h2><br>
<!-- php to calcuate the employee net pay -->
<?php
{
{
if ($dependents = 0)
{
$taxRate = 25;
$calculateTaxRate = 0.25;
}
else if ($dependents >= 1 && $dependents <= 3)
{
$taxRate = 20;
$calculateTaxRate = 0.20;
}
else if ($dependents >= 4 && $dependents <= 6)
{
$taxRate = 10;
$calculateTaxRate = 0.10;
}
else if ($dependents > 6)
{
$taxRate = 5;
$calculateTaxRate = 0.05;
}
echo "Employee's hourly rate of pay = $". "<b>". $hourlyRate. "</b>". " per hour". "<br> ";
echo "Number of hours worked that week = ". "<b>". $hoursWorked. "</b>". " hours". "<br> ";
echo "Number of dependents employee has = ". "<b>". $dependents. "</b>". " dependents". "<br> ";
$overtimeHours = ($hoursWorked - 45);
echo "Number of overtime hours = ". "<b>". $hoursWorked. " - 45 = ". $overtimeHours. "</b>". " hours". "<br> ";
$firstFourtyFive = ($hourlyRate * 45);
echo "First 45 hours pay = ". "<b>". "$". $hourlyRate. " x 45 = $". $firstFourtyFive. "</b>". "<br> ";
$overtime = ($hourlyRate * $overtimeHours * 1.5);
echo "Next ". $overtimeHours. " hours pay (overtime) = ". "<b>". "$". $hourlyRate. " x ". $overtimeHours. " x 1.5 = $". $overtime. "</b>". "<br> ";
$grossPay = ($firstFourtyFive + $overtime);
echo "Gross pay = ". "<b>". "$". $firstFourtyFive. " + ". "$". $overtime. " = $". $grossPay. "</b>". "<br> ";
echo "Tax rate for ". "<b>". $dependents. "</b>". " dependents = ". "<b>". $taxRate. "</b>". "<br> ".
$tax = ($grossPay * $calculateTaxRate);
$taxDeduction = ($tax + $grossPay);
echo "Tax deduction = ". "<b>". $grossPay. " x ". $taxRate. "% = $". $taxDeduction. "</b>". "<br> ";
$netPay = ($grossPay - $taxDeduction);
echo "Net pay = ". "<b>". "$". $grossPay. " - $". $taxDeduction. " = $". $netPay. "</b><br> ";
echo "This employee earned: <b>". $netPay. "</b> this week.". "<br> ";
}
}
?>
<br><br>
<!-- below is to display the back to previous page button -->
  <a href="index.php">back to first page</a>
<br><br>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.