Jump to content

Help with a calculator script


bc2013

Recommended Posts

I am writing a calculator script for a friend of mine but keep getting an error.  I need to pass some values to the handle.php script and then do more calculations in that script. I am new to php and never written a script like this. Thank you for your time. I will paste the scripts below and the error.

 

The HTML page:

<!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=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<br><br>

<center>

<u>Owner Operator Calculator</u><br />

 

<form action="handle_oocalc.php" method="post">

 

Total Miles: <input type="text" name="miles" size="5" />

<br>

Payment: <input type="text" name="payment" size="5" />

<br>

Truck's MPG: <input type="text" name="milesPerGallon" size="5" />

<br>

Fuel Price: <input type="text" name="gasPrice" size="5" />

<br>

 

<input type="submit" name="Submit" value="Calculate" onClick="return vldFrm_Feedback();" />

 

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

 

</form>

</center>

</body>

</html>

 

The handle.php page:

 

<!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=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

 

<body>

 

 

<?php  // handle_oocalc.php

 

// address error handling

ini_set ('display_errors', 1);

error_reporting (E_ALL & ~E_NOTICE);

 

// IN CASE REGISTER_GLOBALS IS DISABLED

$payment = $_POST['payment'] ;

$milesPerGallon = $_POST['milesPerGallon'] ;

$gasPrice = $_POST['gasPrice'] ;

$miles = $_POST['miles'] ;

$gallonsNeeded = $_POST['gallonsNeeded'] ;

$totalFuelCost = $_POST['totalFuelCost'] ;

$netProfit = $_POST['netProfit'] ;

$dollarPerMile = $_POST['dollarPerMile'] ;

 

 

 

 

 

// Calculate the gallons needed for the trip

$gallonsNeeded = $miles / milesPerGallon;

 

 

// Calculate the total fuel cost

$totalFuelCost = $gallonsNeeded * $gasPrice;

 

 

// Calculate net profit

$netProfit = $payment - $totalFuelCost;

 

 

// Calculate dollar per mile

$dollarPerMile = $payment / $miles;

 

 

// Apply the proper formatting

$gallonsNeeded = number_format ($gallonsNeeded, 2);

$totalFuelCost = number_format ($totalFuelCost, 2);

$netProfit = number_format ($netProfit, 2);

$dollarPerMile = number_format ($dollarPerMile, 2);

 

 

// Print out the results

print " Here are the results:<br />

$gallonsNeeded gallons needed for the trip.<br/>

$totalFuelCost is the total fuel cost. <br/>

$netProfit is your net profit.<br/>

$dollarPerMile is the dollar per mile.<br/>";

 

 

?>

</body>

</html>

 

Here's the error I get:

 

Warning: Division by zero in /home/lvfoo0/public_html/handle_oocalc.php on line 33

Here are the results:

0.00 gallons needed for the trip.

0.00 is the total fuel cost.

1,000.00 is your net profit.

0.83 is the dollar per mile.

 

Again, thank you for your time.

Link to comment
https://forums.phpfreaks.com/topic/195267-help-with-a-calculator-script/
Share on other sites

first off

$gallonsNeeded = $miles / milesPerGallon;

 

needs to be:

 

$gallonsNeeded = $miles / $milesPerGallon;

 

also try checking if empty or anything that would make it equal zero

 

if(empty($miles) && empty($milesPerGallon))
{
print("sorry but you do not have enough miles or miles per gallon");
}

of course you can use or || instead of and &&  or checks either one while and && checks both of them at once.

 

you can put it anywhere after you have defined both variables i suppose.

 

if($miles==''||empty($miles)) {
// echo error message here.
}

if($milesPerGallon==''||empty($milesPerGallon))
{
//echo error message here
}

Ok I got that and this is how I coded it:

 

if($miles==''||empty($miles))

{

print("You left the miles field blank.");

}

 

if($milesPerGallon==''||empty($milesPerGallon))

{

print("You left the MPG field blank.");

}

 

if($payment==''||empty($payment))

{

print("You left the payment field blank.");

}

 

if($gasPrice==''||empty($gasPrice))

{

print("You left the fuel price field blank.");

}

 

Now how can I code it so it will basically kill the script? Because now it is printing the error message that I told it to output but it is still doing the calcultions. I guess what I'm asking is how do I just get it to print that message and put a "back" link to the previous page? Thanks again

why don't you put the form inside a PHP variable then echo out the error and the form that way it echos out on the same page :)

 

example:

 

$form="
<form action="handle_oocalc.php" method="post">

Total Miles: <input type="text" name="miles" size="5" />
<br>
Payment: <input type="text" name="payment" size="5" />
<br>
Truck's MPG: <input type="text" name="milesPerGallon" size="5" />
<br>
Fuel Price: <input type="text" name="gasPrice" size="5" />
<br>

<input type="submit" name="Submit" value="Calculate" onClick="return vldFrm_Feedback();" />

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

</form>";

if($miles==''||empty($miles))
   {
   print("You left the miles field blank.$form");
   }

if($milesPerGallon==''||empty($milesPerGallon))
   {
   print("You left the MPG field blank.$form");
   }

if($payment==''||empty($payment))
   {
   print("You left the payment field blank.$form");
   }

if($gasPrice==''||empty($gasPrice))
   {
   print("You left the fuel price field blank.$form");
   }

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.