Page 1 of 1

"$" sign is interfering with form calculations.

Posted: Tue Dec 09, 2008 6:02 pm
by cedartree
I have a form that gets filled out with financial information.
Some of my clients decide to use $ signs before entering a numeric value.

This is fine however the form that gets filled out does a calculation at the end and posts the results via email submission.
When the customer uses the "$" symbol, the calculations do not work and I end up with a "0".

How can I correct this issue to ignore the "$" or restrict them from trying to use it??

here is my code, i will spare you the redundancies:

Code: Select all

 
<?php
 
include('class.Email.php');
 
$total_contents .=  "Groceries/Supplies: " . $_POST['GroceriesSupplies'] . "\n\n";
$total_contents .=  "Day Care: " . $_POST['DayCare'] . "\n\n";
$total_contents .=  "Medical: " . $_POST['Medical'] . "\n\n";
$total_contents .=  "*******Total Expenses*******             " . "\n\n"; 
$total_contents .=  "TOTAL EXPENSES: " . $_POST['GroceriesSupplies'] +(int)$_POST['DayCare']+(int)$_POST['Medical']. "\n\n";
//echo $total_contents ; 
 
 
$to="email@email.com";
$subject="Form submission";
$notify_subject = "Form Submission";
 
$from = "email@email.com";
$notify_message = "There has been a feedback";
$visitor_message = "Thanks for writing to us. We will get back to you at the earliest. 
                Hoping of a long term business relationship with you.
                ";  
 
mail( "email@email.com", "$notify_subject", "$total_contents", "$from");
//mail( "$to", "$notify_subject", "$total_contents", "$from");
header("Location:thank-you.html");
exit();
?>
 

Re: "$" sign is interfering with form calculations.

Posted: Tue Dec 09, 2008 6:10 pm
by invertrio
Try removing the $ using regex:

Code: Select all

$_POST['GroceriesSupplies'] = floatval( preg_replace( '/\$/', '', $_POST['GroceriesSupplies'] ) );
$_POST['DayCare'] = floatval( preg_replace( '/\$/', '', $_POST['DayCare'] ) );
$_POST['Medical'] = floatval( preg_replace( '/\$/', '', $_POST['Medical'] ) );
I also made use of the floatval() function since I think casting it using (int) will drop the decimal values.