Filtering out comma's with PHP
14-07-2004, 16:07
|
#1
|
|
cf.mega poster
Join Date: Jul 2003
Location: Derry
Posts: 7,597
|
Filtering out comma's with PHP
I have a form with two price fields, if the customer enters a number over 500000, the form's target page forwards you on to another page telling you to contact the office for any enquiries over £500,000. The trouble is, if they enter an amount over 500000 with comma's, e.g. 750,000, the forms target page thinks it's 750 not 750000 and carries on trying to do it's bit. Is there an easy way to filter out the comma's on the target page before it does it's price detection bit?
Source for the form page:
Code:
<form action="test.php" method="Post">
<table border="0" cellspacing="0" cellpadding="3">
<tr bgcolor="F1C440">
<td colspan="2" class="style10"><strong>Purchasing</strong></td>
</tr>
<tr bgcolor="#eeeeee">
<td class="style10">Name</td>
<td><input name="name" type="text" /></td>
</tr>
<tr bgcolor="#eeeeee">
<td><span class="style10">Purchase Price<br>
(No comma's)
</span></td>
<td><input name="price" type="text" value="0" /></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2"><span class="style10">
<input name="type" type="radio" value="freehold" checked />
Freehold
<input type="radio" name="type" value="leasehold" />
Leasehold</span></td>
</tr>
<tr bgcolor="#eeeeee">
<td class="style10">Selling Price<br>
(No comma's) </td>
<td><input name="sprice" type="text" id="sprice" value="0" /></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2"><span class="style10">
<input name="stype" type="radio" value="freehold" checked />
Freehold
<input type="radio" name="stype" value="leasehold" />
Leasehold
</span></td>
</tr>
<tr bgcolor="#eeeeee">
<td><span class="style5"> <input name="submit" type="submit" />
</span></td>
<td><span class="style6"></span></td>
</tr>
</table>
</form>
Source for the target page:
Code:
<?php
$toprice = $sprice + $price;
if ($toprice < 1)
{
header('Location: noprice.php');
}
else if ($price > 500000)
{
header('Location: contact.php');
}
else if ($sprice > 500000)
{
header('Location: contact.php');
}
?>
<?php
$officecopy = 8;
$landregistrysearch = 4;
$localsearch = 170;
$banktransfer = 35.25;
$watersearch = 39.95;
$environmentsearch = 40;
$bankruptcysearch = 2;
$indemnity = 94;
if (((Double)$price)>500000)
$d = 420;
else if (((Double)$price)>200000)
$d = 220;
else if(((Double)$price)>100000)
$d = 150;
else if(((Double)$price)>80000)
$d = 100;
else if(((Double)$price)>50000)
$d = 60;
else if(((Double)$price)>0)
$d = 40;
if ($type=="freehold" )
{
$f = 495;
}
else
{
$f = 550;
}
if ($stype=="freehold" )
{
$sf = 450;
}
else
{
$sf = 550;
}
?>
<style type="text/css">
<!--
.style18 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; color: #000000; }
.style20 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; }
.style22 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; }
.style23 {font-size: 10px}
a:link {
color: #000000;
}
a:visited {
color: #000000;
}
a:hover {
color: #000000;
}
a:active {
color: #000000;
}
-->
</style>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
<title>Online Conveyancing Quotation</title>
<p align="center"><span class="style20"><a href=# onclick="window.print();">Print</a> this quote Click <a href="#" onClick="history.go(-1);">here</a> to go back </span></p>
<table border="0" align="center" cellpadding="6" cellspacing="0">
<tr>
<td colspan="2" bgcolor="#F1C440"><div align="center" class="style20">Estimate Prepared For </div></td>
</tr>
<tr bgcolor="#eeeeee">
<td class="style20"><strong>Name:</strong></td>
<td class="style22"><span class="style18"><?php echo $name; ?></span></td>
</tr>
<tr bgcolor="#dddddd">
<td class="style20"><strong>Date:</strong></td>
<td class="style22"><span class="style18"><?php echo date("d /"); echo date("m /"); echo date("Y ");?></span></td>
</tr>
</table>
<br />
<table width="80%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr bgcolor="#F1C440">
<td width="423" bgcolor="#F1C440"><span class="style18">Purchase Price
</span></td>
<td><span class="style22">
<?php
if ($price > 0)
{
echo "£"; echo $price;
}
else
{
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr bgcolor="#F1C440">
<td class="style20">Selling Price </td>
<td><span class="style22">
<?php
if ($sprice > 0)
{
echo "£"; echo $sprice;
}
else
{
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr bgcolor="#000000">
<td colspan="2" height=1></td>
</tr>
<tr bgcolor="#eeeeee">
<td><span class="style20">Legal costs on </span></td>
<td></td>
</tr>
<tr>
<td><span class="style20">
Purchasing <?php
if ($type=="freehold" and $price > 0)
{
echo "- Freehold fee";
}
else if ($type=="leasehold" and $price > 0)
{
echo "- Leasehold fee";
}
else
{
echo "";
}
?>
</span></td>
<td><span class="style22"><?php
if ($type=="freehold" AND $price > 0)
{
echo "£"; printf ("%01.2f", $f);
}
else if ($type=="leasehold" AND $price > 0)
{
echo "£"; printf ("%01.2f", $f);
}
else
{
$f = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr>
<td class="style20">Selling
<?php
if ($stype=="freehold" and $sprice > 0)
{
echo "- Freehold fee";
}
else if ($stype=="leasehold" and $sprice > 0)
{
echo "- Leasehold fee";
}
else
{
echo "";
}
?></td>
<td><span class="style22"><?php
if ($stype=="freehold" AND $sprice > 0)
{
echo "£"; printf ("%01.2f", $sf);
}
else if ($stype=="leasehold" AND $sprice > 0)
{
echo "£"; printf ("%01.2f", $sf);
}
else
{
$sf = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr bgcolor="#F1C440">
<td class="style20">Subtotal A </td>
<td class="style22">£<?php
$subtotala = ($f + $sf);
printf ($subtotala);
?>.00</td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2" class="style20">Payments for bank and insurance charges</td>
</tr>
<tr>
<td class="style20">Bank Charges for Telegraphic Transfer Fee (each)</td>
<td><span class="style22"><?PHP echo "£"; printf ("%01.2f", $banktransfer);
?>
</span></td>
</tr>
<tr>
<td class="style20">Contribution to Professional Indemnity Insurance</td>
<td class="style22"><?PHP echo "£"; printf ("%01.2f", $indemnity);
?></td>
</tr>
<tr bgcolor="#F1C440">
<td class="style20">Subtotal B </td>
<td><span class="style22">£<?php
$subtotalb = ($banktransfer + $indemnity);
printf ($subtotalb);
?>
</span></td>
</tr>
<tr bgcolor="#eeeeee">
<td colspan="2" class="style20">Disbursements payable to the government, local authority or other agencies in respect of Land Registry Fees, VAT, Stamp Duty Land Tax, search fees and other charges . These may be subject to alteration if the rates of Stamp Duty Land Tax or if the fees are changed or varied after the date of this estimate.</td>
</tr>
<tr>
<td class="style20">V.A.T on our charges</td>
<td><span class="style22">£<?php
$feetotal = $f + $sf;
$vat = ($feetotal/100)*17.5;
printf ("%01.2f", $vat);
?>
</span></td>
</tr>
<tr>
<td><span class="style20"><a href="#" onClick="MM_openBrWindow('stampduty.php','','width=350,height=192')">Stamp Duty Land Tax </a></span></td>
<td><span class="style22">
<?php
if (((Double)$price)>500000)
$sd = ((Double)$price/100)*4;
else if (((Double)$price)>250000)
$sd = ((Double)$price/100)*3;
else if (((Double)$price)>60000)
$sd = ((Double)$price/100)*1;
else if (((Double)$price)<60000)
$sd = 0;
?>
<?php
if ($price > 0)
{
echo "£"; printf ("%01.2f", $sd);
}
else
{
echo "Not Applicable";
}
?></span><span class="style23"></span></td>
</tr>
<tr>
<td class="style20"><a href="#" onClick="MM_openBrWindow('landregistryfee.php','','width=350,height=137')">Land Registry Fees </a></td>
<td><span class="style22"><?php
if ($price > 0)
{
echo "£"; printf ("%01.2f", $d);
}
else
{
$d = 0;
echo "Not Applicable";
}
?></span></td>
</tr>
<tr>
<td><span class="style20"><a href="#" onClick="MM_openBrWindow('bankruptcysearch.php','','width=350,height=100')">Bankruptcy Search</a> (each name) </span></td>
<td><span class="style22">
<?php
if ($price > 0)
{
echo "£"; printf ("%01.2f", $bankruptcysearch);
}
else
{
$bankruptcysearch = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr>
<td class="style20"><a href="#" onClick="MM_openBrWindow('localauthoritysearch.php','','width=350,height=225')">Local Authority Search</a> (Approximate fee)</td>
<td><span class="style22">
<?php
if ($price > 0)
{
echo "£"; printf ("%01.2f", $localsearch);
}
else
{
$localsearch = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr>
<td><span class="style20"><a href="#" onClick="MM_openBrWindow('officecopies.php','','width=350,height=138')">Office Copies</a> </span></td>
<td><span class="style22">
<?php
if ($sprice > 0)
{
echo "£"; printf ("%01.2f", $officecopy);
}
else
{
$officecopy = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr>
<td><span class="style20"><a href="#" onClick="MM_openBrWindow('landregistrysearch.php','','width=350,height=152')">Land Registry Search</a> </span></td>
<td><span class="style22">
<?php
if ($price > 0)
{
echo "£"; printf ("%01.2f", $landregistrysearch);
}
else
{
$landregistrysearch = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr>
<td class="style20"><a href="#" onClick="MM_openBrWindow('watersearch.php','','width=350,height=153')">Water Search</a> (optional)</td>
<td><span class="style22">
<?php
if ($price > 0)
{
echo "£"; echo $watersearch;
}
else
{
$watersearch = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr>
<td class="style20"><a href="#" onClick="MM_openBrWindow('environmentalsearch.php','','width=350,height=126')">Environmental Search</a> (optional) </td>
<td><span class="style22">
<?php
if ($price > 0)
{
echo "£"; printf ("%01.2f", $environmentsearch);
}
else
{
$environmentsearch = 0;
echo "Not Applicable";
}
?>
</span></td>
</tr>
<tr bgcolor="#F1C440">
<td><span class="style20">Sub Total C </span></td>
<td><span class="style22">£<?php
$subtotalc = $watersearch + $environmentsearch + $vat + $sd + $d + $bankruptcysearch + $localsearch + $officecopy + $landregistrysearch;
printf ("%01.2f", $subtotalc); ?>
</span></td>
</tr>
<tr bgcolor="F1C440">
<td colspan="2" bgcolor="#FFFFFF"><div align="center" class="style20"><br>
</div></td>
</tr>
<tr bgcolor="#F1C440">
<td colspan="2"><div align="center" class="style20">SUMMARY</div></td>
</tr>
<tr bgcolor="F1C440">
<td bgcolor="#FFFFFF" class="style20">Our Fees (subtotal A)</td>
<td bgcolor="#FFFFFF"><span class="style22">
£<?php
$subtotala = ($f + $sf);
printf ($subtotala);
?>.00</span></td>
</tr>
<tr bgcolor="F1C440">
<td bgcolor="#FFFFFF" class="style20">Other Payments (subtotal B) </td>
<td bgcolor="#FFFFFF"><span class="style22">£<?php
$subtotalb = ($banktransfer + $indemnity);
printf ($subtotalb);
?>
</span></td>
</tr>
<tr bgcolor="F1C440">
<td bgcolor="#FFFFFF" class="style20">Other Payments (subtotal C) </td>
<td bgcolor="#FFFFFF"><span class="style22">£<?php
$subtotalc = $vat + $sd + $d + $bankruptcysearch + $localsearch + $officecopy + $landregistrysearch + $watersearch + $environmentsearch;
printf ("%01.2f", $subtotalc); ?>
</span></td>
</tr>
<tr bgcolor="#eeeeee">
<td class="style20">Total fees and disbursements </td>
<td><span class="style22">£<?php
$finaltotal = $subtotala + $subtotalb + $subtotalc;
printf ("%01.2f", $finaltotal); ?>
</span></td>
</tr>
<tr bgcolor="F1C440">
<td colspan="2" bgcolor="#FFFFFF" class="style22"><strong><br>
Notes</strong><br>
<br>
In a Leasehold sale a further £50.00 plus VAT may be charged for dealing with your Landlords in obtaining replies to a Leasehold Questionnaire. In some Leasehold cases there may be additional Solicitor's Costs, Land Registry fees and Stamp Duty Land Tax where a new lease/or extended lease is being granted by the Landlord. <br>
<br>
Extra fees may be payable to comply with the lease conditions such as registration fees for notices and/or obtaining a Licence to Assign or a Deed of Covenant. <br>
<br>
Any further Bank Telegraphic Transfers required will be charged for at £30.00 plus VAT. This may be where the mortgagee requires the mortgage to be redeemed by Telegraphic Transfer, or if you request the sale proceeds to be sent direct to your bank. <br>
<br>
This Estimate has been prepared on the basis of information supplied by you.
We reserve the right to revise the estimate where additional work is required, such as, redemption of more than one mortgage, providing undertakings to others, drafting of Trust or other Deeds. The estimate also assumes that title is not defective and does not require work to remedy any defects. <br>
<br>
<p>We may at our discretion make an additional charge of £80.00 plus VAT if completion has to take place less than 10 working days after exchange of contracts. </p>
<p>We are obliged to take out compulsory indemnity insurance which means we are covered for any possible claims that may arise and you are protected by comprehensive professional indemnity insurance. We have included this charge as a contribution towards the cost of that cover. </p></td>
</tr>
<tr bgcolor="F1C440">
<td colspan="2" bgcolor="#FFFFFF"><div align="center"><span class="style20"><a href=# onclick="window.print();"><br>
Print</a> this quote Click <a href="#" onClick="history.go(-1);">here</a> to go back </span></div></td>
</tr>
</table>
<br>
</body>
</html>
TIA
|
|
|
14-07-2004, 16:24
|
#2
|
|
Anyone can play guitar
Join Date: Jun 2003
Location: London way
Age: 32
Services: Women for money
Posts: 6,234
|
Re: Filtering out comma's with PHP
at the risk of sounding like a broken record (my answers always seem to involve java script)....
you could just put an onchange event on the text field that filters out any commas...
i'll paste in example code in a minute
|
|
|
14-07-2004, 16:33
|
#3
|
|
Anyone can play guitar
Join Date: Jun 2003
Location: London way
Age: 32
Services: Women for money
Posts: 6,234
|
Re: Filtering out comma's with PHP
Code:
<input name="price" type="text" value="0" onChange="this.value=this.value.replace(/,/g,'')"/>
will remove all the ',' from a field - basically /,/g is a regular expression to matching any ','s in the string - the g bit says do it all over the string, not just the first occurance.
|
|
|
14-07-2004, 16:34
|
#4
|
|
cf.mega poster
Join Date: Jul 2003
Location: Derry
Posts: 7,597
|
Re: Filtering out comma's with PHP
I'll give that a quick go
Last edited by Bifta; 14-07-2004 at 16:38.
|
|
|
14-07-2004, 16:37
|
#5
|
|
Anyone can play guitar
Join Date: Jun 2003
Location: London way
Age: 32
Services: Women for money
Posts: 6,234
|
Re: Filtering out comma's with PHP
The bit above just uses a change event on the field - is that no good?
Edit : gaaaa! clashing edits!
Last edited by Mr_love_monkey; 14-07-2004 at 16:40.
|
|
|
14-07-2004, 16:40
|
#6
|
|
cf.mega poster
Join Date: Jul 2003
Location: Derry
Posts: 7,597
|
Re: Filtering out comma's with PHP
seems to work perfectly, cheers!
|
|
|
14-07-2004, 16:44
|
#7
|
|
Anyone can play guitar
Join Date: Jun 2003
Location: London way
Age: 32
Services: Women for money
Posts: 6,234
|
Re: Filtering out comma's with PHP
Glad to hear it's fine
Don't know if this will be of any use to you later, but this is from one of the
javascript books I've got on the pattern matching
Quote:
When you specify a regular expression, it will by default only match the first occurrence that it encounters. It is possible to specify a pattern to match that can occur several times in a line. To match all of these, you can use the 'g' attribute. The 'g' stands for global matching.
There is one other attribute that allows the matching to be carried out in a case-insensitive manner. That is the 'i' attribute.
Both of these attributes are placed after the closing slash at the end of the pattern.
The case-insensitive match works like this:
/javascript/i
would match the following strings
JavaScript
javascript
JAVASCRIPT
JaVaScRiPt
The g attribute applied like this, would cause the pattern:
/0/g
to match every zero in the string:
'0100, 00123, "00067", 666000'
There is another attribute to control whether the pattern is applied to single lines or multiple lines. Because the regular expression is realized as an object, this is controlled by means of an object property accessed via the multi-line identifier, the letter 'm' in JavaScript 1.5 (shipped with Netscape 6.0)
Here is a list of the available attributes:
g Global match for every occurrence of the pattern throughout the line.
i Case-insensitive matching.
ig Case-insensitive global match.
m Multiple lines to be processed. Available only in JavaScript 1.5 and mutually exclusive with the 's' attribute.
gm Global match on multiple lines.
im Case-insensitive matching on multiple lines.
igm Case-insensitive global match on multiple lines.
|
|
|
|
14-07-2004, 17:00
|
#8
|
|
&amp;nbsp;
Join Date: Nov 2003
Location: Leeds - the dog house
Age: 31
Services: Email me for a current price list
Posts: 8,241
|
Re: Filtering out comma's with PHP
$string_with_no_commas = number_format($price, 2, '.', '')
'' is two ' and not "
That should do it. Methinks it's the implicit conversion when you add the two prices that's at fault.
I also ltrim and rtrim the input string just in case.
Use switch/case instead of loads of if/else
I really wouldn't use Javascript - if the user disables Javascript then your site probably won't work. Client side data formatting and verification is for speed only - should always implement the same measures server side too.
__________________
Consistency is the last refuge of the unimaginative [Wilde]
|
|
|
14-07-2004, 17:08
|
#9
|
|
Anyone can play guitar
Join Date: Jun 2003
Location: London way
Age: 32
Services: Women for money
Posts: 6,234
|
Re: Filtering out comma's with PHP
Don't you diss javascript! - that's fighting talk where I come from
Seriously, What you say makes sense - my use of javascript comes from force of habit and producing a product at work that has always required javascript to be activated - but it is always backed up with server side verification.
|
|
|
14-07-2004, 17:17
|
#10
|
|
&amp;nbsp;
Join Date: Nov 2003
Location: Leeds - the dog house
Age: 31
Services: Email me for a current price list
Posts: 8,241
|
Re: Filtering out comma's with PHP
Quote:
|
Originally Posted by Mr_love_monkey
Don't you diss javascript! - that's fighting talk where I come from 
|
It does have a role to play but more in the "fuzzy stuff" to do with keeping visitors/customers (happy) by providing a speedy site. If you can do client side verification, then that's great because it means the visitor/customer doesn't have to sit there waiting for the server to reject their input, which equals one unhappy bunny  But you should always, always program/script with total mistrust of any data that comes to the server from the wild. No assumptions. That includes things like the URL (can be modified) and the path to a script (e.g. what happens if script (a) is called direct?).
__________________
Consistency is the last refuge of the unimaginative [Wilde]
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +1. The time now is 23:31.
|