Simple PHP HTML Input Validation for Form

2020-02-29 php html

Noob question: When I test the input validation of my first php file, it still takes me to the second php file through the form action and I am not sure how to make the program continue to ask for the correct input until it takes the user to the second php file through the form action.

<html lang = "en">
    <body>
        <?php 
            $incomeErr = "";                                                //set empty variables
            $income = $status = "";                                         
            function test_input($data) {        
                $data = trim($data);                                        
                $data = stripslashes($data);                                
                $data = htmlspecialchars($data);                            
                return $data;
            }
        ?>

        <form action = "taxcalc.php" method = "post">                       
            <fieldset>
                <p>
                    <label>Filing Status: </label>
                    <select id = "Filing Status" select name = "status">    
                        <option value = "single">Single</option>
                        <option value = "married">Married</option>
                    </select>
                </p>
                Tax: 
                <input type = "number" name = "income" value = "">          <!--income number box-->
                <?php 
                    if (empty($_POST["income"])) {                          //checks if income variable is empty
                        $incomeErr = "*income is required";                 //if income box does not contain a numeric value
                        echo "$incomeErr";                                  //outputs income error message
                    } 
                    else {
                        $income = test_input($_POST["income"]);             //runs test_input function with income var
                    } 
                ?>
                <br><br>
                <input type = "submit" value = "Calculate">                 <!--submit values-->
            </fieldset>
        </form>
    </body>
</html>

This is the second php file. Probably unnecessary but if there is any feedback it would be appreciated.

<html>
<head>
    <title>TAX RESULTS</title>
</head>
<body>
<?php
$taxDeduction = $newTax = true;
$income = $_REQUEST['income'];                  
$status = $_REQUEST['status'];
if($status == "single"){
    echo "Filing Status: Single<br>";
}
else if($status == "married"){
    echo "Filing Status: Married<br>";
}
echo "Taxable Income: $$income<br>";
if($status == "single" && $income >= 415051){
    $taxDeduction = ($income * .396) + 120529.75;
    $newTax = $income - $taxDeduction;
}
else if($status == "single" && ($income <= 415050 && $income >= 413351)){
    $taxDeduction = ($income * .35) + 119934.75;
    $newTax = $income - $taxDeduction;
}
else if($status == "single" && ($income <= 413350 && $income >= 190151)){
    $taxDeduction = ($income * .33) + 46278.75;
    $newTax = $income - $taxDeduction;
}
else if($status == "single" && ($income <= 190150 && $income >= 91151)){
    $taxDeduction = ($income * .28) + 18558.75;
    $newTax = $income - $taxDeduction;
}
else if($status == "single" && ($income <= 91150 && $income >= 37651)){
    $taxDeduction = ($income * .25) + 5183.75;
    $newTax = $income - $taxDeduction;
}
else if($status == "single" && ($income <= 37650 && $income >= 9276)){
    $taxDeduction = ($income * .15) + 927.5;
    $newTax = $income - $taxDeduction;
}
else if($status == "single" && ($income <= 9275 && $income >= 0)){
    $taxDeduction = $income * .1;
    $newTax = $income - ($income * .1);
}
if($status == "married" && $income >= 464851){
    $taxDeduction = ($income * .396) + 129996.5;
    $newTax = $income - $taxDeduction;
}
else if($status == "married" && ($income <= 464850 && $income >= 411501)){
    $taxDeduction = ($income * .35) + 111324;
    $newTax = $income - $taxDeduction;
}
else if($status == "married" && ($income <= 411500 && $income >= 230451)){
    $taxDeduction = ($income * .33) + 51577.5;
    $newTax = $income - $taxDeduction;
}
else if($status == "married" && ($income <= 230450 && $income >= 151201)){
    $taxDeduction = ($income * .28) + 29387.5;
    $newTax = $income - $taxDeduction;
}
else if($status == "married" && ($income <= 151200 && $income >= 74901)){
    $taxDeduction = ($income * .25) + 10312.5;
    $newTax = $income - $taxDeduction;
}
else if($status == "married" && ($income <= 74900 && $income >= 18451)){
    $taxDeduction = ($income * .15) + 1845;
    $newTax = $income - $taxDeduction;
}
else if($status == "married" && ($income <= 18450 && $income >= 0)){
    $taxDeduction = $income * .1;
    $newTax = $income - $taxDeduction;
}
?>
<table style="width:50%">
  <tr>
    <th>Filing Status</th>
    <th>Taxable Income</th> 
    <th>Tax Rate</th>
    <th>Income Tax</th>
  </tr>
  <tr>
    <td><?php echo "$status";?></td>            <!-- prints filing status -->
    <td><?php echo "$$income";?></td>           <!-- prints income -->
    <td><?php echo "$$taxDeduction";?></td>     <!-- prints tax deduction -->
    <td><?php echo "$$newTax";?></td>           <!-- prints new tax -->
  </tr>
</table>

</body>
</html>

Answers

Try

<?php 
            $incomeErr = "";                                                //set empty variables
            $income = $status = "";                                         
            function test_input($data) {        
                $data = trim($data);                                        
                $data = stripslashes($data);                                
                $data = htmlspecialchars($data);                            
                return $data;
            }
          if (empty($_POST["submit"])) { // check if form submitted
            if (empty($_POST["income"])) {                          //checks if income variable is empty
                        $incomeErr = "*income is required";                 //if income box does not contain a numeric value
                                                          //outputs income error message
                    } 
                    else {
                        $_POST["income"] = test_input($_POST["income"]);             //runs test_input function with income var
                        include "taxcalc.php";die();
                    } 
          }
          }
                ?>
<html lang = "en">
    <body>
        <form method = "post">                       
            <fieldset>
                <p>
                    <label>Filing Status: </label>
                    <select id = "Filing Status" select name = "status">    
                        <option value = "single">Single</option>
                        <option value = "married">Married</option>
                    </select>
                </p>
                Tax: 
                <input type = "number" name = "income" value = "">          <!--income number box-->
                <?php echo $incomeErr ?>
                <br><br>
                <input type = "submit" name= "submit" value = "Calculate">                 <!--submit values-->
            </fieldset>
        </form>
    </body>
</html>

Related