Jump to content

HELP with functions and calculations!!


bluez26

Recommended Posts

Hey I'm new to php ... obviously lol ... anyway I'm having a hard time getting this code to work. It's for a hw assignment in my class and we have to use a function to calculate a part of a form. Every time I try to incorporate this function/call it or whatever I end up getting a blank page when I click the submit button on the page. Someone please help me! I am completely lost here.

 

function HourlyRate($_GET["THrs"], $_GET["Rate"]){

$TotHrs = $_GET["THrs"];

$PRate = $_GET["Rate"];

If($TotHrs <= "40") $HRate = ($TotHrs * $PRate);

else

$HRate = ("40" * $PRate) + (($TotHrs - "40") * ($PRate * "1.5"));

echo $HRate;

}

 

echo "<p>Employee Last Name: ", $_GET["EmpName"], "</p>";

echo "<p>Employee SSN: ", $_GET["EmpSSN"], "</p>";

echo "<p>Employee Status: ", $_GET["EmpStat"], "</p>";

echo "<p>Week Beginning: ", $_GET["BDate"], "</p>";

echo "<p>Hours Worked: ", $_GET["THrs"], "</p>";

echo "<p>Pay Rate: ", $_GET["Rate"], "</p>";

echo "<p>Gross Pay: ";

If ($_GET["EmpStat"] == "Exempt") echo $_GET["THrs"] * $_GET["Rate"]

else HourlyRate($_GET["THrs"], $_GET["Rate"]);

 

 

Please help!

Link to comment
Share on other sites

Post your Form

 

Try

 

function HourlyRate($thrs,$rate){
  $TotHrs = $thrs;
  $PRate = $rate;
  If($TotHrs <= 40){ 
         $HRate = ($TotHrs * $PRate);
           } else {
                  $HRate = (40 * $PRate) + (($TotHrs - 40) * ($PRate * 1.5));
            }  return $HRate;
         }

  $hrate = HourlyRate($_GET['THrs'],$_GET['Rate']);
echo $hrate;
  

Link to comment
Share on other sites

It still won't work :(

 

I changed my function like you have it there and put this in as part of the IF statement and it still gives me a blank page...

 

If ($_GET["EmpStat"] == "Exempt") echo $_GET["THrs"] * $_GET["Rate"]

else $hrate = HourlyRate($_GET['THrs'],$_GET['Rate']);

 

echo $hrate;

 

 

I'm not using any $_POST or anything like that in my form, just the "get" method.

Link to comment
Share on other sites

i think we still need your FORM code (submittion code).

 

I think the problem (without the form code) is that your trying to use $_GET variables (these variables are in the browser URL as a "Query String")

 

Most Form use "Method=post", therefor you should change all of your $_GET vars to $_POST vars as btherl explained earlier.

Link to comment
Share on other sites

Well my teacher left us the source code for the form and he's using the get method but here's what i have:

 

<form method="get" action="Proj2.php">

<table width="600px"><tr><td>

<strong>Employee Name:</strong>

</td><td colspan="3">

<input name="EmpName" type="text" style="width:300px;" />

</td></tr>

<tr><td>

<strong>Employee SSN:</strong>

</td><td>

<input name="EmpSSN" type="text" style="width:100px;" />

</td><td>

<strong>Employee Status:</strong>

</td><td>

<input name="EmpStat" type="radio" value="Exempt" checked="checked" style="border:0px;" /> Exempt    

<input name="EmpStat" type="radio" value="Hourly" style="border:0px;" />Hourly

</td></tr>

<tr><td>

<strong>Week Begining:</strong>

</td><td>

<input name="BDate" type="text" style="width:100px;" />

</td><td>

<strong>Total Hours:</strong>

</td><td>

<input name="THrs" type="text" style="width:100px;" />

</td></tr>

<tr><td>

<strong>Pay Rate:</strong>

</td><td>

<input name="Rate" type="text" style="width:100px;" />

</td></tr>

<tr><td colspan="4">

<input name="reset" type="reset" value="Reset" />    

<input name="submit" type="submit" value="Submit" />

</td></tr>

</table>

</form>

 

Link to comment
Share on other sites

try

 

<?php
function HourlyRate($thrs,$rate){
  $TotHrs = $thrs;
  $PRate = $rate;
  If($TotHrs <= 40){ 
        $HRate = ($TotHrs * $PRate);
          } else {
                 $HRate = (40 * $PRate) + (($TotHrs - 40) * ($PRate * 1.5));
            }  return $HRate;
        }




echo "<p>Employee Last Name: ", $_POST["EmpName"], "</p>";
echo "<p>Employee SSN: ", $_POST["EmpSSN"], "</p>";
echo "<p>Employee Status: ", $_POST["EmpStat"], "</p>";
echo "<p>Week Beginning: ", $_POST["BDate"], "</p>";
echo "<p>Hours Worked: ", $_POST["THrs"], "</p>";
echo "<p>Pay Rate: ", $_POST["Rate"], "</p>";
echo "<p>Gross Pay: "; 
     If ($_POST["EmpStat"] == "Exempt"){ 

echo $_POST["THrs"] * $_POST["Rate"];


}
        else{  

$hrate = HourlyRate($_POST['THrs'],$_POST['Rate']);
echo $hrate;


}

 ?> 

Link to comment
Share on other sites

That's gotten me somewhere but it's not pulling the information from the form now.. do I have to change something in my form ?

 

Change form to

 

<form method="post" action="Proj2.php">
<table width="600px"><tr><td>
<strong>Employee Name:</strong>
</td><td colspan="3">
<input name="EmpName" type="text" style="width:300px;" />
</td></tr>
<tr><td>
<strong>Employee SSN:</strong>
</td><td>
<input name="EmpSSN" type="text" style="width:100px;" />
</td><td>
<strong>Employee Status:</strong>
</td><td>
<input name="EmpStat" type="radio" value="Exempt" checked="checked" style="border:0px;" /> Exempt    
<input name="EmpStat" type="radio" value="Hourly" style="border:0px;" />Hourly
</td></tr>
<tr><td>
<strong>Week Begining:</strong>
</td><td>
<input name="BDate" type="text" style="width:100px;" />
</td><td>
<strong>Total Hours:</strong>
</td><td>
<input name="THrs" type="text" style="width:100px;" />
</td></tr>
<tr><td>
<strong>Pay Rate:</strong>
</td><td>
<input name="Rate" type="text" style="width:100px;" />
</td></tr>
<tr><td colspan="4">
<input name="reset" type="reset" value="Reset" />    
<input name="submit" type="submit" value="Submit" />
</td></tr>
</table>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.