1,105,386 Community Members

PHP script help with calculator

Member Avatar
newbie37
Newbie Poster
9 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am stuck. I have a beginning php class and have run into a wall. Any help will be appreciated.
here is the from that is submitted for paycheck calculation:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h2>Paycheck Calculation</h2>
<form id="form1" name="form1" method="get" action="paycheck.php">

<table width="40%" border="0" cellspacing="1" cellpadding="1">
<tr>
<th width="30%" scope="row">Hours Worked:</th>
<td width="20%"><label>
<input type="text" name="hoursWorked" id="hours" />
</label></td>
</tr>
<tr>
<th scope="row">Wages:</th>
<td><label>
<input type="text" name="wagesRate" id="wages" />
</label></td>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<td><label>
<input type="submit" name="pay" id="pay" value="Submit" />
</label></td>
</tr>
</table>


</form>
<p>&nbsp;</p>
</body>
</html>

here is the PHP code that i have so far but I am stuck.
<?PHP
$hoursWorked = $_REQUEST["hoursWorked"]
$wages = $_REQUEST["wagesRate"]
$pay = $_REQUEST["pay"]
$payCheck = $_REQUEST["payCheck"]
$x = $hoursWorked;
$y = $wages;
$z = $paycheck;
$z = ($x *$y) + (($x - 40) * $y * 1.5)
echo "Your Paycheck is: $payCheck";
}
?>
thanks in advance.

Member Avatar
kireol
Posting Whiz
305 posts since Mar 2008
Reputation Points: 10 [?]
Q&As Helped to Solve: 53 [?]
Skill Endorsements: 0 [?]
 
0
 

hi. Welcome to daniweb.


As a tip, when asking for help, ask direct questions such as "Why am I getting error xxxx" or "What is the function to yyyyy".

We're good here, but not mind readers

Member Avatar
newbie37
Newbie Poster
9 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

here is the error message I recieve:
PHP Parse error: parse error, unexpected T_VARIABLE in D:\Inetpub\Ciswebs\CIS54\SACHSE_SHARON\wk_10\paycheck.php on line 11

Line 11 is: $wages = $_REQUEST["wagesRate"]
I believe the math equation is correct but do I have the function stated properly?

Member Avatar
FlashCreations
Posting Whiz
393 posts since Sep 2008
Reputation Points: 20 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 3 [?]
 
0
 

Try this, you were missing some semicolon's at the end of your lines:

paycheck.php:

<html>
<head>
<title>Paycheck Calculator</title>
</head>
<body>
<?php
if($_GET['submit']=="Submit"&&isset($_GET["hoursWorked"])&&isset($_GET["wagesRate"]))
{
  echo "Your Paycheck is: <b>".($_GET["hoursWorked"] * $_GET["wagesRate"]) + (($_GET["hoursWorked"] - 40) * $_GET["wagesRate"] * 1.5)."</b><br/>\n<a href='paycheck.php'>Calculate Again</a>";
}
else
{
  echo "<form action='paycheck.php' method='get'>\n <label for='hoursWorked'>Hours Worked: </label><input type='text' name='hoursWorked' value='".$_GET["hoursWorked"]."' /><br/>\n <label for='wagesRate'>Wage: </label><input type='text' name='wagesRate' value='".$_GET["wagesRate"]."' /><br/>\n <input type='submit' name='submit' value='Submit' />\n</form>";
}
?>
</body>
</html>

I would also suggest being more specific about getting variable from forms. Use $_GET[] for get requests and use $_POST[] for post requests. Also, there is one more problem involving the $paycheck variable. You set the x variable to be the value of paycheck, then you do some calculations, but only show the original paycheck variable. Did you mean to set paycheck to the value of the calculations and then show that variable?

Member Avatar
newbie37
Newbie Poster
9 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the suggestion. Here is the error I am now recieving :
PHP Notice: Undefined index: submit in D:\.........\paycheck\paycheck.php on line 10
The form (html) is filled in and submitted and the printed response should be the gross amount for the hours worked.
The code you suggested actually returned the form with the text fields filled in. I hope I am explaining this correctly. Sorry again for all the questions. The teacher is of now help.

Member Avatar
FlashCreations
Posting Whiz
393 posts since Sep 2008
Reputation Points: 20 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 3 [?]
 
0
 

Alright then, have you replaced all your files (for this project) with one file called paycheck.php and added only my code to it. Line 10 is the end of the first if block. I tried it on my XAMPP and it worked perfectly and didn't have any errors. (And the reason why the form is being filled out again is because a value is missing...it simply fills in the values you gave it and leaves the other required ones blank). On a side note: Don't worry about all the questions. When you are new to a language it's hard at first, but you'll get it eventually and maybe answer people's questions on DaniWeb!

EDIT:
One little fix that I just remembered. It shouldn't fix your error, but it should fix and output problem:

<html>
<head>
<title>Paycheck Calculator</title>
</head>
<body>
<?php
//If the form has been submitted
if($_GET['submit']=="Submit"&&isset($_GET["hoursWorked"])&&isset($_GET["wagesRate"]))
{
	//Return the paycheck and give user the option to calculate again
	echo "Your Paycheck is: <b>".(($_GET["hoursWorked"] * $_GET["wagesRate"]) + (($_GET["hoursWorked"] - 40) * $_GET["wagesRate"] * 1.5))."</b><br/>\n<a href='paycheck.php'>Calculate Again</a>";
}
else
{
	//Else return the form with the values that need to be filled
	echo "<form action='paycheck.php' method='get'>\n <label for='hoursWorked'>Hours Worked: </label><input type='text' name='hoursWorked' value='".$_GET["hoursWorked"]."' /><br/>\n <label for='wagesRate'>Wage: </label><input type='text' name='wagesRate' value='".$_GET["wagesRate"]."' /><br/>\n <input type='submit' name='submit' value='Submit' />\n</form>";
}
?>
</body>
</html>
Question Answered as of 4 Years Ago by FlashCreations and kireol
Member Avatar
newbie37
Newbie Poster
9 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you for pointing me in the right direction with GET and RESULT. This is really good :)
here is how I wrote the syntax in the end:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?PHP
function payCheck() {
    $hoursWorked = $_GET["hoursWorked"];
    $wageRate = $_GET["wageRate"]; 
    {
    $x = $hoursWorked;
    $y = $wageRate;
    if ($_GET["hoursWorked"] <= "40")
    $payCheck = ($x * $y);
    else
    $payCheck = ($x * $y) + (($x - 40) * ($y * 1.5));
}
echo "Your Paycheck is: $ $payCheck";
}
payCheck();
?>
</body>
</html>
Member Avatar
muralikalpana
Posting Pro
549 posts since Sep 2009
Reputation Points: 17 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
-1
 

I am stuck. I have a beginning php class and have run into a wall. Any help will be appreciated.
here is the from that is submitted for paycheck calculation:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h2>Paycheck Calculation</h2>
<form id="form1" name="form1" method="get" action="paycheck.php">

<table width="40%" border="0" cellspacing="1" cellpadding="1">
<tr>
<th width="30%" scope="row">Hours Worked:</th>
<td width="20%"><label>
<input type="text" name="hoursWorked" id="hours" />
</label></td>
</tr>
<tr>
<th scope="row">Wages:</th>
<td><label>
<input type="text" name="wagesRate" id="wages" />
</label></td>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<td><label>
<input type="submit" name="pay" id="pay" value="Submit" />
</label></td>
</tr>
</table>


</form>
<p>&nbsp;</p>
</body>
</html>

here is the PHP code that i have so far but I am stuck.
<?PHP
$hoursWorked = $_REQUEST["hoursWorked"]
$wages = $_REQUEST["wagesRate"]
$pay = $_REQUEST["pay"]
$payCheck = $_REQUEST["payCheck"]
$x = $hoursWorked;
$y = $wages;
$z = $paycheck;
$z = ($x *$y) + (($x - 40) * $y * 1.5)
echo "Your Paycheck is: $payCheck";
}
?>
thanks in advance.

you are not included paycheck in form.

<?PHP
if($_POST)
{
$hoursWorked = $_POST["hoursWorked"];
$wages = $_POST["wagesRate"];
$pay = $_POST["pay"];
$payCheck = $_POST["payCheck"];
$x = $hoursWorked;
$y = $wages;
$z = $paycheck;
$z = ($x *$y) + (($x - 40) * $y * 1.5);
echo $z;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h2>Paycheck Calculation</h2>
<form id="form1" name="form1" method="post">

<table width="40%" border="0" cellspacing="1" cellpadding="1">
<tr>
<th width="30%" scope="row">Hours Worked:</th>
<td width="20%"><label>
<input type="text" name="hoursWorked" id="hours" />
</label></td>
</tr>
<tr>
<th scope="row">Wages:</th>
<td><label>
<input type="text" name="wagesRate" id="wages" />
</label></td>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<td><label>
<input type="submit" name="pay" id="pay" value="Submit" />
</label></td>
</tr>
</table>


</form>
<p>&nbsp;</p>
</body>
</html>
Member Avatar
muralikalpana
Posting Pro
549 posts since Sep 2009
Reputation Points: 17 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
-1
 

<?PHP


$pay = $_REQUEST["pay"];


?>

<tr>
<th scope="row">&nbsp;</th>
<td><label>
<input type="submit" name="pay" id="pay" value="Submit" />
</label></td>
</tr>

i donot know why you taken $_request[pay].
there is no need to request submit.

Member Avatar
FlashCreations
Posting Whiz
393 posts since Sep 2008
Reputation Points: 20 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 3 [?]
 
0
 

i donot know why you taken $_request[pay].
there is no need to request submit.

Unless you want to check for a form submission with a single paged script:

<?php
if($_GET['submit']=="Submit")
{
  //Process form
}
else
{
  //Print Form
  echo "<input type="submit" name="submit" value="Submit" />"
}
?>

But, you're right. In that instance it wasn't used properly.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article