Program to calculate a paycheck- won't work but no errors

Hi,

I am very very very new to PHP. I have a program that is meant for a user to put in hours worked and wages and the program will calculate the total and display. Only problem is it won’t work but I’m not getting any errors (using NetBeans). I realize my code is probably messy and I might be doing too much, but I’m using the “PHP Programming with MySQL” 2nd edition to guide me and it is no help. I would appreciate any help. I’m not asking you to do my homework, I’m just stuck. Thank you.

[php]
function displayRequired($fieldName) {
echo “The field “$fieldName” is required to calculate the wages earned.
\n”;
}

    function validateInput($data, $fieldName) {
        global $errorCount;
        if (empty($data)) {
            displayRequired($fieldName);
            ++$errorCount;
            $retval = "";
        } else { 
            $retval= trim($data);
            $retval = stripslashes($retval);
        }
    }  
      $errorCount = 0;
      
    $hoursworked = "";
    $wage = "";
    $DisplayForm = TRUE;
    
    if ($errorCount>0){
        echo "Please use the back button to re-enter the data. <br />\n";
    } 
    
    $regularpay= ($hoursworked * $wage);
    $extrapay = ($hoursworked * $wage) + (($hoursworked -40) * $wage * .5);
    
    
    if (isset($_POST["Submit"])) {
        $hoursworked = $_POST["Hours"];
        $wage = $_POST["Wages"];
        if (is_numeric($hoursworked) && is_numeric($wage)) {
            $DisplayForm = FALSE;
        } else {
            $DisplayForm= TRUE;
        }
    }
    if ($DisplayForm) {
        ?>
    <form name="PaycheckForm" action="PaycheckFrailey.php"
        method="post">
        <p>Enter your hours worked: <input type="text" name="Hours" 
               value ="<?php echo $hoursworked; ?>" /><br />
            Hourly Wage: <input type="text" name="Wages" 
                value ="<?php echo $wage; ?>" /><br /></p>
        <p><input type ="submit" name="Submit"  value="Send Form" />
    </form>	
    <?php
    }
    
    if (isset($_POST ["Submit"])) {
        $hoursworked= filter_input(\INPUT_POST, "Hours");
        $wage = filter_input(\INPUT_POST, "Wage");
        $errorCount ="";
        if (is_numeric($hoursworked) && is_numeric ($wage)) {
            if ($hoursworked <=40) { // Calculate regular pay
                $regularpay = ($hoursworked * $wage);
            } else { // Calculate time and half
                $extrapay = ($hoursworked * $wage) + ((hoursworked -40) * $wage * .5);
            }
        }
      
        $totalpay = $regularpay + $extrapay;
    echo "<p>Your paycheck is worth \$$totalpay. </p>"; }[/php]

[php]if (isset($_POST[“Submit”])) {
$hoursworked= filter_input(\INPUT_POST, “Hours”);
$wage = filter_input(\INPUT_POST, “Wages”);
$errorCount ="";
if (is_numeric($hoursworked) && is_numeric ($wage)) {
if ($hoursworked <=40) { // Calculate regular pay
$regularpay = ($hoursworked * $wage);
} else { // Calculate time and half
$extrapay = ($hoursworked * $wage) + (($hoursworked -40) * $wage * .5);
}
}[/php]

You had hoursworked which needed to be made a variable with $
Also $wage = filter_input(\INPUT_POST, “Wage”); needed to be “Wages” to match your input field name.

Also use a form of error_reporting such as error_reporting(E_ALL); to help out.
Also, not a big deal, but try to be constant with your code, eg. $hoursworked -40 would be $hoursworked - 40 with the space.
I find it best to use nice structuring throughout code so any oddities are easier to jump out. Just my opinion ofcourse

Excellent piece of advice :wink:

I agree lol - in any maths equation

10 - 5 is quite different than
10 - -5

Sponsor our Newsletter | Privacy Policy | Terms of Service