PHP Form

This is my second program and I keep getting the below error. I am hoping someone can help me before I throw my laptop. Thanks in advance.
Notice: Undefined index: hours in C:\xampp\htdocs\CHPT4\paycheck.php on line 12
Notice: Undefined index: wage in C:\xampp\htdocs\CHPT4\paycheck.php on line 13
Hours Worked:
Standard Pay:0
Overtime Pay:-60
Your total paycheck is: $-60

It seems like it is telling me that wage and hours aren’t declared in the HTML portion but to me it looks like they are:

<!DOCTYPE htm PUBLIC "-//W3c//DTD XHTML 1.0 Strict//EN" /* Paycheck Form * /

Paycheck Form

First Name:

Last Name:

Hourly Wage:

Hours Worked:

   

Also, this is the PHP form portion:

<!DOCTYPE htm PUBLIC "-//W3c//DTD XHTML 1.0 Strict//EN" /* Paycheck PHP * / <?php

$hours = $_GET[“hours”];
$wages = $_GET[“wage”];

$overtime = ($hours - 40)1.5;
$spay = ($hours <= 40)
$wages;

echo “Hours Worked:” . $hours . “
”;
echo “Standard Pay:” . $spay . “
”;
echo “Overtime Pay:” . $overtime . “
”;
echo “Your total paycheck is: $” . number_format($spay + $overtime). “
”;

?>

That isn’t all in the same file is it?

You are passing by get, so in the url when you pass the form, do the variables pass or is it just the page?

Hi Jen R

Test your PHP form in your browser by calling the URL: http://localhost/paycheck.php?hours=10&wage=50

Replace localhost with your IP or computername or just leave it.

If you get what is expected then you can move on to troubleshooting the HTML part which is doing the posting.

Checking your Apache or NGINX log will also point you in the right direction of what is getting sent to your script.

Well, first, you have many errors in your code. Most likely this is the issue with your error messages.

For a start, a web page is in this format…

... ... You have your body inside your headers. This will never work correctly.

Also, you never check to see if the inputs actually exist. If you do not have ?hours= in the posted page,
you will never give your $hours a value and it will fail as undefined. You need to check if the variable exists
and if not, echo out an error. You can use the PHP function “ISSET()” to test if the $_GET[] variable is there
or not. If a user presses the submit button without entering data in those fields, you get an error.

Example:
if (isset($_GET[“hours”] {
// Hours input field is set…
$hours = $_GET[“hours”];
} else {
// No data for Hours…
echo “Sorry, you did not enter your number of hours!”;
}

Normally, you would check if you got all inputs first and display an error if one is empty.
Hope that helps…

PS: Ditto on Astonecipher’s comment, is this one page or two?

Assuming this is two individual files, I would take this one step further:

file the form is processed on:
[php]if ( !isset( $_GET ) ) { // change this to
header( ‘location: {redirect to file for form}’);
} else {
// process form data
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service