Re: Calculate total amount from input

From: Date: Wed, 01 Oct 2003 09:16:21 +0000
Subject: Re: Calculate total amount from input
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-164506@lists.php.net to get a copy of this message
irinchiang@justeducation.com wrote:
Hi all,
       A PHP question to ask: I am using PHP to do a calculation of the total of
fields where user actually input the amount into the text field and I will calculate the TOTAL from the fields as well as the GST. Wonder how should I go about doing that? I did something like :
           $total = $_POST["field 1"] + $_POST["field 2"] + $_POST["field 3"];
           $total = number_format($total, 2, ".", " ");
           echo "$$total<br>";
           echo "$".$total."<br>";
but it wasn't successful.
What do you mean it wasn't successful? What did you get on the screen? Are you sure that the fields are being referenced correctly? Something like this would use some error checking : $total = 0; $errors = ""; if($_POST["field 1"]) { $total += intval($_POST["field 1"]); } else { $errors .= "Field 1 not entered"; } if($_POST["field 2"]) { $total += intval($_POST["field 2"]); } else { $errors .= "Field 2 not entered"; } if($_POST["field 3"]) { $total += intval($_POST["field 3"]); } else { $errors .= "Field 3 not entered"; } if ($errors != "") { echo "Errors : ".$errors; } $total = number_format($total, 2, '.', ''); echo "Total is \$ ".$total; -- Burhan Khalid phplist[at]meidomus[dot]com http://www.meidomus.com

« previous php.general (#164506) next »