PHP Form Validation Help


Hi all,

I am working on a project and wondering if I could get a little help with my code. I am building a paycheck calculator and need to have that calculator work off 2 pages, an HTML p[age with a 2 field form that a user inputs weekly hours worked and their hourly rate, then on the PHP page I need to calculate weekly pay and factor in any overtime. I also need to verify and validate the form entry data to provide error messages if need be. Only one I can think of is if the user doesn't input an integer. I put together a code I thought would work but it's not... Any and all input is greatly appreciated. Currently all this is doing for entering "abc" is giving the letters a value of 0.

$hours = (int) $_GET["hours"];
$wage = (int) $_GET["wage"];

if (is_int($hours) && is_int($wage)) {
$hours = (int) $_GET["hours"];
$wage = (int) $_GET["wage"];
} else {
echo "Input was not an integer.";

  $overtime = max($hours - 40, 0);
  $paycheck = ($overtime * $wage * 1.5);
  $paycheck += ($hours - $overtime) * $wage;

Thanks again!


The first step I would do is check whether a value is set at all, this is really good to do for most checks. I like to do isset(), but I also see some people just put code in try catch blocks to see whether they work or not.

So i'd suggest that first.

$house = isset($_GET["hours"]) ? $_GET["hours"] : null;


if( isset($_GET["hours"]) && is_int($_GET["hours"])  ) { $hours = $_GET["hours"]; }
else {}

For your wage, you want to also check for floating numbers. Remember int's are whole numbers. What if they work 25.25 hours? That is pretty common. Also check that hours & wages is a positive number too.

Only place the processing code in a successful if statement, don't leave it open to process incorrect information. You're only going to do anything if the conditions are proper.


I will give this a shot and see what I came up with. Thank you for your help!


So far I'm not having much luck...

if( isset($_GET["hours"]) && is_int($_GET["hours"])  ) { $hours = $_GET["hours"]; }
else {
  echo "Entry not valid";
if( isset($_GET["wage"]) && is_int($_GET["wage"])  ) { $wage = $_GET["wage"]; }
  else {
    echo "Entry not valid";

Returning multiple errors, even when I enter valid entries.

Screenshot of errors: Here


Final Update:

Got the code to work and behave how I need it to. Thanks for your help emgo!!


Your welcome! Glad you were able to work through it ; )

Keep in touch if you have any further questions.