Hello. I need to create a calculator with php. I did it but I\'m having some iss
ID: 3855793 • Letter: H
Question
Hello.
I need to create a calculator with php. I did it but I'm having some issues with:
1. If the user fails to enter the Hours Worked, or if the Hours Worked are less than 0, or if the Hours Worked are greather than 80, the following message should be returned: "you must enter hours worked between 0 and 80 to be paid."
2. If the user fails to enter the Hourly Rate, or if the Hourly Rate is less than 7.25, or greater than 100.00, the following message should be returned: "You must enter an hourly rate that is between 7.25 and 100.00 to be paid."
3. If the user fails to enter more than one of the values such as both Hours Worked and Hourly Rate, messages show for each item not entered with the “Please fill out the fields as required and submit again” message.
4. I need to get the error message under the form, but instead I get it on top of it.
I'll paste here what I have in the code.
Please help!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="Styles.css">
<title>Paycheck2 </title>
<style>
#table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 70%;
}
#table td, #table th {
border: 1px solid #ddd;
padding: 5px;
border-color: #9ACD32;
margin-right: 0px;
}
#table tr:nth-child(even){background-color: #EBF1D5 ;}
#table tr:hover {background-color: #ddd;}
#table th {
padding-top: 6px;
padding-bottom: 6px;
text-align: left;
background-color: #9ACD32;
color: white;
border-collapse: collapse;
}
</style>
</head>
<body>
<?php # Script paycheck
$page_title = 'Paycheck Calculator';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Minimal form validation:
if (isset($_POST['fname'], $_POST['lname'], $_POST['hworked']) &&
is_numeric($_POST['hworked']) && is_numeric($_POST['hrate'])) {
$overtime ='10';
$fica ='5.65';
$state ='5.75';
$federal ='28.00';
$hworked = '';
$regular_pay = $_POST['hworked'] * $_POST['hrate'];
$overtime_pay = $overtime * 1.5 * $_POST['hrate'];
$gross_pay = $regular_pay + $overtime_pay;
// calculating the tax withhelds
$ficataxwithheld = ($gross_pay * $fica)/100;
$statetaxwithheld = $gross_pay * $state/100;
$federaltaxwithheld = $gross_pay * $federal/100;
$total_taxes = $ficataxwithheld + $statetaxwithheld + $federaltaxwithheld;
$net_pay = $gross_pay - $total_taxes;
// applying number format AFTER claculations
$ficataxwithheld = number_format ($ficataxwithheld, 2);
$statetaxwithheld = number_format ($statetaxwithheld,2);
$federaltaxwithheld = number_format ($federaltaxwithheld, 2);
$regular_pay = number_format ($regular_pay, 2);
$overtime_pay = number_format ($overtime_pay, 2);
$gross_pay = number_format ($gross_pay, 2);
$total_taxes = number_format ($total_taxes, 2);
if ( !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['hworked']) && !empty($_POST['hrate']) ) {
echo "<table id="table"><tr><th> Paycheck Calculator </th><th></th></tr>";
echo "<tr><td>Employee Name </td><td> {$_POST['fname']} {$_POST['lname']}</td></tr>";
echo "<tr><td>Regular Hours worked (between 0 and 80)</td><td> {$_POST['hworked']}</td></tr>";
echo "<tr><td>Overtime Hours worked (between 0 and 40)</td><td> $overtime</td></tr>";
echo "<tr><td>Hourly Rate(between 0 and 100.00)</td><td> {$_POST['hrate']}</td></tr>";
echo "<tr><td>Regular Pay </td><td> $$regular_pay </td></tr>";
echo "<tr><td>Overtime Pay </td><td> $$overtime_pay </td></tr>";
echo "<tr><td>Gross Pay </td><td> $$gross_pay </td></tr>";
echo "<tr><td>FICA Tax Rate (ex. 5.65) </td><td> $fica% </td></tr>";
echo "<tr><td>FICA Tax Withheld </td><td> $$ficataxwithheld </td></tr>";
echo "<tr><td>State Tax Rate (ex. 5.75) </td><td> $state% </td></tr>";
echo "<tr><td>State Tax Withheld </td><td> $$statetaxwithheld </td></tr>";
echo "<tr><td>Federal Tax Rate (ex. 20.00) </td><td> $federal% </td></tr>";
echo "<tr><td>Federal Tax Withheld </td><td> $$federaltaxwithheld </td></tr>";
echo "<tr><td>Total Taxes</td><td> $$total_taxes </td></tr>";
echo "<tr><td>Net Pay </td><td> $$net_pay </td></tr></table>";}
if (!empty($_POST['fname'])) {
$name = $_POST['fname'];
}
else{
$name = NULL;
echo '<p>You need to enter your first name to be paid!</p>'; }
if (!empty($_POST['lname'])) {
$name = $_POST['lname']; }
else{
$name = NULL;
echo '<p>You need to enter your last name to be paid!</p>'; }
if(!empty($_POST['hworked'])) {
$hworked = $_POST['hworked'];
}
else{
$hworked = NULL;
echo '<p>Please enter a number between 0 and 80</p>';
}
if($_POST['hrate'] >= 0 and $_POST['hrate'] <= 100)
{
$hrate= $_POST['hrate'];
}
else{
$hrate = NULL;
echo '<p>Please enter a number between 0 and 100</p>';
}
}
}
?>
<form action="paycheck3.php" method="post">
<fieldset>Use the form to calculate the Regular Pay, Overtime Pay, Gross etc for an employee.<br>
<label> First Name <br><input type="text" name="fname" value="<?php if (isset($_POST['fname'])) echo $_POST['fname']; ?>"/></label><br>
<label> Last Name <br><input type="text" name="lname" value="<?php if (isset($_POST['lname'])) echo $_POST['lname']; ?>"/></label><br>
<label> Hours Worked (between 0 and 80) <br><input type="text" name="hworked" value="<?php if (isset($_POST['hworked'])) echo $_POST['hworked']; ?>"/></label><br>
<label> Hourly Rate (between 0 and 100) <br><input type="text" name="hrate" value="<?php if (isset($_POST['hrate'])) echo $_POST['hrate']; ?>"/></label><br>
<p><input type="submit" name="submit" value="Calculate!" /></p>
</fieldset>
</form>
</body>
</html>
Explanation / Answer
The code in itself is correct. You just need to make a few changes of placing the code in the right places,I've highlighted them in bold-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="Styles.css">
<title>Paycheck2 </title>
<style>
#table {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 70%;
}
#table td, #table th {
border: 1px solid #ddd;
padding: 5px;
border-color: #9ACD32;
margin-right: 0px;
}
#table tr:nth-child(even){background-color: #EBF1D5 ;}
#table tr:hover {background-color: #ddd;}
#table th {
padding-top: 6px;
padding-bottom: 6px;
text-align: left;
background-color: #9ACD32;
color: white;
border-collapse: collapse;
}
</style>
<!--<script type="text/javascript"> You can use this to validate as well using onclick() in submit
function empty() {
var x,y,z,a;
x = document.getElementById("fname").value;
y=document.getElementById("lname").value;
z=document.getElementById("hworked").value;
a=document.getElementById("hrate").value;
if (x == "") {
alert("Enter First Name");
return false;
};
else if (y == "") {
alert("Enter Last Name");
return false;
};
else if (z == "") {
alert("Enter Hours Worked ");
return false;
};
else if (a == "") {
alert("Enter Hourly Pay");
return false;
};
}
</script>
-->
</head>
<body>
<?php # Script paycheck
$page_title = 'Paycheck Calculator';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Minimal form validation:
if (isset($_POST['fname'], $_POST['lname'], $_POST['hworked']) &&
is_numeric($_POST['hworked']) && is_numeric($_POST['hrate'])) {
$overtime ='10';
$fica ='5.65';
$state ='5.75';
$federal ='28.00';
$hworked = '';
if (!empty($_POST['fname'])) {
$name = $_POST['fname'];
}
else{
$name = NULL;
echo '<p>You need to enter your first name to be paid!</p>'; }
if (!empty($_POST['lname'])) {
$name = $_POST['lname']; }
else{
$name = NULL;
echo '<p>You need to enter your last name to be paid!</p>'; }
if(!empty($_POST['hworked']) && $_POST['hworked']>=0 && $_POST['hworked']<=80 ) {
$hworked = $_POST['hworked'];
}
else{
$hworked = 0; //instead of hworked = NULL as the calculations will not show 0.
echo '<p>"you must enter hours worked between 0 and 80 to be paid.</p>';
}
if($_POST['hrate'] >= 7.25 and $_POST['hrate'] <= 100)
{
$hrate= $_POST['hrate'];
}
else{
$hrate = 0;
echo '<p>You must enter an hourly rate that is between 7.25 and 100.00 to be paid.</p>';
}
$regular_pay = $hrate * $hworked; //using the req variables instead of $_POST
$overtime_pay = $overtime * 1.5 * $hrate;
$gross_pay = $regular_pay + $overtime_pay;
// calculating the tax withhelds
$ficataxwithheld = ($gross_pay * $fica)/100;
$statetaxwithheld = $gross_pay * $state/100;
$federaltaxwithheld = $gross_pay * $federal/100;
$total_taxes = $ficataxwithheld + $statetaxwithheld + $federaltaxwithheld;
$net_pay = $gross_pay - $total_taxes;
// applying number format AFTER claculations
$ficataxwithheld = number_format ($ficataxwithheld, 2);
$statetaxwithheld = number_format ($statetaxwithheld,2);
$federaltaxwithheld = number_format ($federaltaxwithheld, 2);
$regular_pay = number_format ($regular_pay, 2);
$overtime_pay = number_format ($overtime_pay, 2);
$gross_pay = number_format ($gross_pay, 2);
$total_taxes = number_format ($total_taxes, 2);
if ( !empty($_POST['fname']) && !empty($_POST['lname']) && !empty($_POST['hworked']) && !empty($_POST['hrate']) ) {
echo "<table id="table"><tr><th> Paycheck Calculator </th><th></th></tr>";
echo "<tr><td>Employee Name </td><td> {$_POST['fname']} {$_POST['lname']}</td></tr>";
echo "<tr><td>Regular Hours worked (between 0 and 80)</td><td> {$_POST['hworked']}</td></tr>";
echo "<tr><td>Overtime Hours worked (between 0 and 40)</td><td> $overtime</td></tr>";
echo "<tr><td>Hourly Rate(between 7.25 and 100.00)</td><td> {$_POST['hrate']}</td></tr>";
echo "<tr><td>Regular Pay </td><td> $$regular_pay </td></tr>";
echo "<tr><td>Overtime Pay </td><td> $$overtime_pay </td></tr>";
echo "<tr><td>Gross Pay </td><td> $$gross_pay </td></tr>";
echo "<tr><td>FICA Tax Rate (ex. 5.65) </td><td> $fica% </td></tr>";
echo "<tr><td>FICA Tax Withheld </td><td> $$ficataxwithheld </td></tr>";
echo "<tr><td>State Tax Rate (ex. 5.75) </td><td> $state% </td></tr>";
echo "<tr><td>State Tax Withheld </td><td> $$statetaxwithheld </td></tr>";
echo "<tr><td>Federal Tax Rate (ex. 20.00) </td><td> $federal% </td></tr>";
echo "<tr><td>Federal Tax Withheld </td><td> $$federaltaxwithheld </td></tr>";
echo "<tr><td>Total Taxes</td><td> $$total_taxes </td></tr>";
echo "<tr><td>Net Pay </td><td> $$net_pay </td></tr></table>"; }
}
}
?>
<form action="ass.php" method="post">
<fieldset>Use the form to calculate the Regular Pay, Overtime Pay, Gross etc for an employee.<br>
<label> First Name <br><input type="text" name="fname" id="fname" value="<?php if (isset($_POST['fname'])) echo $_POST['fname']; ?>"/></label><br>
<label> Last Name <br><input type="text" name="lname" id="lname" value="<?php if (isset($_POST['lname'])) echo $_POST['lname']; ?>"/></label><br>
<label> Hours Worked (between 0 and 80) <br><input type="text" name="hworked" id="hworked" value="<?php if (isset($_POST['hworked'])) echo $_POST['hworked']; ?>"/></label><br>
<label> Hourly Rate (between 0 and 100) <br><input type="text" name="hrate" id="hrate" value="<?php if (isset($_POST['hrate'])) echo $_POST['hrate']; ?>"/></label><br>
<p><input type="submit" name="submit" value="Calculate!" /></p>
</fieldset>
</form>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.