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
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!
#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;
}
$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 "
| Paycheck Calculator | |
|---|---|
| Employee Name | {$_POST['fname']} {$_POST['lname']} |
| Regular Hours worked (between 0 and 80) | {$_POST['hworked']} |
| Overtime Hours worked (between 0 and 40) | $overtime |
| Hourly Rate(between 0 and 100.00) | {$_POST['hrate']} |
| Regular Pay | $$regular_pay |
| Overtime Pay | $$overtime_pay |
| Gross Pay | $$gross_pay |
| FICA Tax Rate (ex. 5.65) | $fica% |
| FICA Tax Withheld | $$ficataxwithheld |
| State Tax Rate (ex. 5.75) | $state% |
| State Tax Withheld | $$statetaxwithheld |
| Federal Tax Rate (ex. 20.00) | $federal% |
| Federal Tax Withheld | $$federaltaxwithheld |
| Total Taxes | $$total_taxes |
| Net Pay | $$net_pay |
if (!empty($_POST['fname'])) {
$name = $_POST['fname'];
}
else{
$name = NULL;
echo '
You need to enter your first name to be paid!
'; }
if (!empty($_POST['lname'])) {
$name = $_POST['lname']; }
else{
$name = NULL;
echo '
You need to enter your last name to be paid!
'; }
if(!empty($_POST['hworked'])) {
$hworked = $_POST['hworked'];
}
else{
$hworked = NULL;
echo '
Please enter a number between 0 and 80
';}
if($_POST['hrate'] >= 0 and $_POST['hrate'] <= 100)
{
$hrate= $_POST['hrate'];
}
else{
$hrate = NULL;
echo '
Please enter a number between 0 and 100
';}
}
}
?>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
