validation not working correctly

Here’s what I’m supposed to do:
Create a two-part form that calculates an employee’s weekly gross salary, based on the number of hours worked and an hourly wage that you choose. Use an HTML document as a Web form with two text boxes—one for the number of hours worked and one for the hourly wage. Use a PHP document as the form handler. Compute any hours over 40 as time-and-a-half. Be sure to verify and validate the submitted form data and provide appropriate error messages for invalid values.
Here’s my HTML file:

[code]

Calculate Your Pay

Number of Hours Worked:

Hourly Wage:

  [/code]

Here’s my PHP file:
[php]<?php
$hoursWorked = validateInput ($_POST[‘Hours’], “Hours”);
$wages = validateInput($_POST[‘Wage’], “Wage”);

if ($errorCount>0) {
echo “Please enter the information.
\n”;
redisplayForm($hoursWorked, $wages);
}
if (is_numeric($hoursWorked) && is_numeric($wages)){
if ($hoursWorked <= 40)
{
$payCheck = ($hoursWorked * $wages);
echo “

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

”;
}
if ($hoursWorked > 40)
{
$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
echo “

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

”;
}
}
else{
echo “

Only numbers can be used. Press the back button to try again.

”;
//displayrequired($fieldName);
//(is_string($hoursWorked) && is_numeric($wages)==0)
//redisplayForm($hourWorked, $wages);
}

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{
return ($data);
}

}
$errorCount=0;

function redisplayForm($hoursWorked, $wages) {
?>

Number of Hours Worked:
Hourly Wage:

  

<?php }

?>[/php]

The problem is when I try to validate incorrect data. Thanks in advance for any help

Well, SpeaknSpell,

First, you did not close your form…

Also, I have never placed functions at the bottom of code. Usually, they must be BEFORE they are used.
I would try that also.

Lastly, you never increment the error number. "++variable " is used for string incrementing. You should use “variable++” to increment integers.

Hope this helps… Post the results and your new code either way…

Thanks for the info, ErnieAlex. I just didn’t “grab” the when I copied my code (sorry 'bout that). So I moved my if statements to be below my functions. The ++errorCount is from our book that I got from the example they gave us. I still get weird errors when it checks the errors. I think there may be an issue with the else statement. I think that’s where the problem lies.
Any other suggestions?

Well, here is a link showing that the ++ issue is correct after the variable.
http://php.net/manual/en/language.operators.increment.php
(Check out the samples showing strings vs integers)

Next, post your new code and the error message you are getting and we will sort it out for you.
I am sure we can sort it out…

I didn’t change a thing in my HTML, but here’s the code anyway:[code]

Calculate Your Pay

Number of Hours Worked:

Hourly Wage:

  

[/code]

I made the change in the increment, (I now realize the original code in the book was using strings) – no more error – YES!

BUT, I wanted to have my form to be a sticky form with the specific errors saying what was missing or necessary to get the function to work properly.
(If you look at the bottom of my code, I’ve attempted this on 3 separate occasion, but I commented them out because I can’t seem to get it right.)

Here’s the PHP code:
[php]

<?php $hoursWorked = validateInput ($_POST['Hours'], "Hours"); $wages = validateInput($_POST['Wage'], "Wage"); 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{ return ($data); } } $errorCount=0; function redisplayForm($hoursWorked, $wages) { ?>

Number of Hours Worked:
Hourly Wage:

  

<?php } if ($errorCount>0) { echo "Please enter the information.
\n"; redisplayForm($hoursWorked, $wages); } if (is_numeric($hoursWorked) && is_numeric($wages)){ if ($hoursWorked <= 40) { $payCheck = ($hoursWorked * $wages); echo "

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

"; } if ($hoursWorked > 40) { $payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5); echo "

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

"; } } else{ echo "

Only numbers can be used. Press the back button to try again.

"; //displayRequired($fieldName); //(is_string($hoursWorked) && is_numeric($wages)==0) //redisplayForm($hourWorked, $wages); }

?>

[/php]

I really appreciate this – I even bought a new book today to get a better “grip” on PHP – I like PHP, I just want to know it better!

Well, glad it is mostly working for you now. A little thing like syntax is very important. I have spent 3 days on an error only to find it was a missing comma! Another note, Google is your buddy, Google is your friend. (As far as PHP goes, that is! LOL) I find if you ask the search correctly you will have more luck. I always start it with “PHP” or “PHP code” and then my question. Lots of help out there in the world. (Of course, lots of help here, too!)

Okay onto error messages… I notice you do some processing and then set up an error count variable.
I usually do it this way. First start off your PHP code with your error-count setting it to zero. Next process ALL your inputs BEFORE posting it back to the browser. Then test it. If the error-count makes it thru the validation tests, then post your output. In general terms, something like this:

$ErrorCount=0;
-accept your input-

test first validation code
if okay, continue to next validation
if failed, $ErrorCount=1; $ErrorMessage=“some message that indicates error”;

test next validation code
if okay, continue to next validation, etc…
if failed, $ErrorCount=1; $ErrorMessage =“Tsk! Tsk! Tsk! message… LOL”;

Now, all validations have been tested...
  if ErrorCount=0  Then post the response with correct outputs...
  else 
     echo "Error in inputs:  Message=" . $ErrorMessage;
     die();    (OR go back and and display message on posting page...)

Well, hope the “general layout” works for you… If not, post some more code… Good luck…

I really appreciate you giving me all that insight. I think my problem is I don’t know when to create a function, where to keep going with the if else statements or … I think I’m just tired of looking at it and I’m getting frustrated and grasping for straws. I’m going to stop now, but I’ll be back at it again in the morning. Maybe sleep will seep it into me.

Been working on for a while and I receive this error:
Parse error: syntax error, unexpected $end in C:\wamp\www\Labs\Chapter.04\Projects\process_Paycheck2.php on line 61
This is what I have now in my php code:
[php]<?php
$hoursWorked = validateInput ($_POST[‘Hours’], “Hours”);
$wages = validateInput($_POST[‘Wage’], “Wage”);

$errorCount=0;

function validateInput($data, $fieldName) {
global $errorCount;
global $hoursWorked;
global $wages;
if (empty($data) || !is_numeric($data)){
$errorCount=1;
echo “Please enter a number into “$fieldName”.
\n”;
}
if ($errorCount>0) {
redisplayForm($hoursWorked, $wages);
}
else{
showPay($hoursWorked, $wages);
}

function showPay ($hoursWorked, $wages) {
global $errorCount;
global $hoursWorked;
global $wages;
if (($errorCount=0) && is_numeric($hoursWorked) && is_numeric($wages)){
if ($hoursWorked <= 40)
{
$payCheck = ($hoursWorked * $wages);
echo “

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

”;
}
if ($hoursWorked > 40)
{
$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
echo “

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

”;
}
}
}

function redisplayForm($hoursWorked, $wages) {
?>

Number of Hours Worked:
Hourly Wage:

  

<?php }

?>

[/php]

Anybody have any suggestions?

Well, let’s see. First, you only showed about 40 lines of code, so not sure what is on line 61!

Usually unexpected “$end” ’ s are usually because you are missing a semicolon at the end of a PHP line!
(Almost always!!!)

So, check that out first. Also, if you are going to redisplay a form with the previously entered data, make sure you also display the error message to the user explaining what they did wrong on the previous post. (I may have missed that in your.)

If you can’t figure out line 61, post it with a few before it and after it…

its because the validate function isn’t terminated. Need another } after the error count statement.

Thank you for all your help. I’ve taken ErnieAlex’s advice and revamped my code to this and hopefully it’s more efficient. If the user enters numbers, I get 2 “your pay is…” AND if the user enters incorrect info, I get the error AND the “your pay is…”.
I’ve tried putting in the last else of the check function: ($hoursWorked > 40), I get Unexpected T_Else.

[php]

Pay Calculations <?php $hoursWorked = validateInput ($_POST['Hours'], "Hours"); $wages = validateInput($_POST['Wage'], "Wage"); $errorCount=0;

if ($errorCount>0) {
redisplayForm($hoursWorked, $wages);
}

function validateInput($data, $fieldName) {
global $errorCount;
global $hoursWorked;
global $wages;

if (empty($data) || !is_numeric($data)){
	$errorCount++;
	redisplayForm($hoursWorked, $wages);
	echo "Please enter a number into \"$fieldName\" text box.<br />\n";
	$retval="";
}
else {
check($hoursWorked, $wages);
}

}
function check($payCheck) {
global $errorCount;
global $hoursWorked;
global $wages;

if ($errorCount==0) {
if ($hoursWorked <= 40) {
$payCheck = ($hoursWorked * $wages);
}
else {
$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
}
}
echo “

Your Wages Earned

You worked $hoursWorked hours at a rate of $wages an hour.


Your pay total is: $payCheck


Try Again?

”;
}

function redisplayForm($hoursWorked, $wages) {
?>

Calculate Your Pay

Number of Hours Worked:

Hourly Wage:

  

<?php }

?>

[/php]

I’ve also tried this code but get "unexpected T_Variable on line 43:
[php]

Pay Calculations <?php $hoursWorked = validateInput ($_POST['Hours'], "Hours"); $wages = validateInput($_POST['Wage'], "Wage"); $errorCount=0;

if ($errorCount>0) {
redisplayForm($hoursWorked, $wages);
}

function validateInput($data, $fieldName) {
global $errorCount;
global $hoursWorked;
global $wages;
$payCheck=0;

if (empty($data) || !is_numeric($data)){
	$errorCount++;
	redisplayForm($hoursWorked, $wages);
	echo "Please enter a number into \"$fieldName\" text box.<br />\n";
	$retval=$data;
}
	else { 
		if (($errorCount==0) && $hoursWorked <= 40){
			$payCheck = ($hoursWorked * $wages);
		}
	}
	else {
		(($errorCount==0) && ($hoursWorked > 40) 
			$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
		}
	
	 echo "<h1>Your Wages Earned</h1><p>You worked <strong>$hoursWorked</strong> hours at a rate of <strong>$wages</strong> an hour.</p> 
			<h3>Your pay total is:  <strong>$payCheck</strong></h3><br /><p><a href='Paycheck.html'>Try Again?</a></p>";

}

function redisplayForm($hoursWorked, $wages) {
?>

Calculate Your Pay

Number of Hours Worked:

Hourly Wage:

  

<?php }

?>

[/php]

that error code usually means a missing ; somewhere, around line 43, whatever that is. you’re also missing the if for this line (($errorCount==0) && ($hoursWorked > 40)

You are also missing the { after that missing IF and compare…

I edited the check function to this: [php]function check($payCheck) {
global $errorCount;
global $hoursWorked;
global $wages;

if ($errorCount==0) {
if ($hoursWorked <= 40) {
$payCheck = ($hoursWorked * $wages);
}
else {
if ($hoursWorked > 40) {
$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
}
}
echo “

Your Wages Earned

You worked $hoursWorked hours at a rate of $wages an hour.


Your pay total is: $payCheck


Try Again?

”;
}
}[/php] I no longer get an error, but I get 2 displays of the echo at the bottom. Any suggestions?

Something doesn’t look right in this code. (That you last posted…)
[php]
function check($payCheck) {
global $errorCount;
global $hoursWorked;
global $wages;
if ($errorCount==0) {
if ($hoursWorked <= 40) {
$payCheck = ($hoursWorked * $wages);
} else {
if ($hoursWorked > 40) {
$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
}
}
echo "

Your Wages Earned

You worked " . $hoursWorked . " hours at a rate of " . $wages . " an hour.

Your pay total is: " . $payCheck . “


Try Again?

”;
}
}
[/php]
Well, I had to reformat it so I could read it. While grouping it to view it easier, I noticed you had variables inside your string. I am sure they were not printed correctly. Note the extra double-quotes I added above.
Let us know if this helps…

Still have the same problem. I’ve already turned in my assignment b/c it was due yesterday. It’s just bugging me that I can’t get it to work right. I would like to know how to actually write the program correctly. I want to get the logic understood in my head before I move on to my next assignment.

Well, Speakn,
Normally, to do this type of program, you would create two pages. The first would be a simple HTML with a form on it. This page would “get” the data needed. It would post into another page. This second would be a PHP file.
Inside the second page would be all the validation and calculations. This page would post the results back to the user. Then, if more calculations are needed it would post back to the original page.

If you want to have us check all your code to see what is wrong with it, post all your pages and all your code and we can help. Since you posted only sections we do not have a full image of what you are trying to do.
(I think you have it nearly done…) By the way, what does your next project involve???

He can keep it on the same page. I think he just needs to return the result instead of echoing it right from the function.
[php]
function check($payCheck) {
global $errorCount;
global $hoursWorked;
global $wages;
if ($errorCount==0) {
if ($hoursWorked <= 40) {
$payCheck = ($hoursWorked * $wages);
} else {
if ($hoursWorked > 40) {
$payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5);
}
}
return $payCheck;
}
}

echo "

Your Wages Earned

You worked " . $hoursWorked . " hours at a rate of " . $wages . " an hour.

Your pay total is: " . check() . “


Try Again?

”;
[/php]

Something like that anyways. Could be why he’s getting doubles, unless in a loop somewhere.

I tried richei’s suggestion and it didn’t work. The whole issue is that I wanted to create a form and if the user enters invalid information, the form retains the invalid information, but an error saying something like, " you need to enter a number in hours field," and the form stays on the screen with the data still in the field. Once the user enters the information correctly, I wanted to display how many hours and at the wages entered to show what they will be paid. I got this code to work except for retaining the data on the error, so I used a message saying "press the back button to try again. " I also think maybe my PHP code isn’t efficient. See my comment in the PHP of : I’m not sure if I should have created a function…

Here’s my HTML file [code]

Pay Calculation

Calculate Your Pay

Number of Hours Worked:

Hourly Wage:

  

[/code]

Here’s my PHP code:[php]

Pay Calculations <?php $hoursWorked = validateInput ($_POST['Hours'], "Hours"); $wages = validateInput($_POST['Wage'], "Wage"); $errorCount=0;

function displayRequired($fieldName) {
echo “The field “$fieldName” is required to calculate the wages earned.
\n”;
}
function validateInput($data, $fieldName) {
global $errorCount;

if (empty($data)) {  //if the field is empty, it calls the display required function, adds to errorCount and returns the value of empty or null; otherwise it shows the data entered in the field
displayRequired($fieldName);
$errorCount++;
$retval = "";
} 	
else{
return ($data);
}

}

function redisplayForm($hoursWorked, $wages) {
?>

Calculate Your Pay

Number of Hours Worked:
Hourly Wage:

  

<?php } //should I have created a function for this? If so, How would I call it? if ($errorCount>0) { echo "Please enter the information.
\n"; redisplayForm($hoursWorked, $wages); } if (is_numeric($hoursWorked) && is_numeric($wages)){ if ($hoursWorked <= 40) { $payCheck = ($hoursWorked * $wages); echo "

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

"; } if ($hoursWorked > 40) { $payCheck = ($hoursWorked * $wages) + (($hoursWorked - 40) * $wages * 1.5); echo "

Your Wages Earned

You worked $hoursWorked hours at a rate of $$wages an hour.

Your pay total is: $$payCheck


Try Again?

"; } }

else{
echo “

Only numbers can be used. Press the Back button and Please try again.

”;
//displayRequired($fieldName);
//(is_string($hoursWorked) && is_numeric($wages)==0)
//redisplayForm($hourWorked, $wages);
}

?>

[/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service