PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2013, 11:47 AM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Price in form-input with format 0000.00

Hello!
I been trying and searching and canīt find a way to pass a price value from form to php to later insert in mysql.
The prices must end with these formats:
1145, 1145.00 or for exampel 1145.50
I thought the easiest would be to have 2 inputs then join them together like this:
PHP Code:
 $holidaycost $price.".".$decimal

then I check using ctype_digit that only numbers been entered.
This works perfect however if I leave the field decimal empty then the check (ctype_digit) gives the errormessage as the form does not send any number.
So I been trying to convert the non-values sent by the form to a 0 but without success.
I appreciate any help.

Reply With Quote
  #2  
Old January 16th, 2013, 12:04 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Leave it in decimal form then use sprintf to format it for your database.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #3  
Old January 16th, 2013, 12:20 PM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Quote:
Originally Posted by gw1500se
Leave it in decimal form then use to format it for your database.


Thanks, but Im afraid I dont understand what you mean.
How should the form input/ be done (1 or 2), and how should it be validated?

This gives me decimal form:
PHP Code:
 $number 123;
$holidaycost sprintf("With 2 decimals: %1\$.2f",$number);
echo 
$holidaycost


But if the price to insert is 123.55 or maybe 1234.55 and they write 1.234.55 or even add € or whatever

Reply With Quote
  #4  
Old January 16th, 2013, 12:37 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Perhaps I am misunderstanding what you asked. Are you trying to convert a currency entry entered on a form to decimal for PHP? It sounded like you were trying to go the other way.

Reply With Quote
  #5  
Old January 16th, 2013, 01:16 PM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Quote:
Originally Posted by gw1500se
Perhaps I am misunderstanding what you asked. Are you trying to convert a currency entry entered on a form to decimal for PHP? It sounded like you were trying to go the other way.


I just want to enter whatever price is inserted in a form, the prices can have decimals or the may have not,
the problem is to validate the input or inputs.

So I am trying to use 2 inputs one for the first value and one for the second value (decimal) then validate that no other character than numbers are inserted with ctype_digit.

However if the price is for exampel 1000 and I leave the decimal field in the form in blank, the validation I do with ctype_digit gives error as the form does not send anything with the field empty.
I have this validation:
PHP Code:
if ( ctype_digit($_POST['price']) &&  ctype_digit($_POST['decimal'])) {
    
$holidaycost $price.".".$decimal

So I tried to convert the empty field to a 0, but canīt find the way.
Maybe this is not the way to go.

Reply With Quote
  #6  
Old January 16th, 2013, 01:29 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
I still am not understanding what the 2 fields represent. Are you trying to split your entry into an integer part and a 2nd field for the decimal part? You are making it hard on yourself and your users. Just let them enter a single field that can be a decimal number.
Comments on this post
Jyncka agrees: Got there just as I was writing my reply, damn

Reply With Quote
  #7  
Old January 16th, 2013, 01:31 PM
Jyncka's Avatar
Jyncka Jyncka is offline
We're trapped inside a game!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: Maryland
Posts: 297 Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 37258 Folding Title: Starter FolderFolding Points: 37258 Folding Title: Starter Folder
Time spent in forums: 5 Days 8 h 35 m 57 sec
Reputation Power: 902
Quote:
Originally Posted by newtonperri
I just want to enter whatever price is inserted in a form, the prices can have decimals or the may have not,
the problem is to validate the input or inputs.

So I am trying to use 2 inputs one for the first value and one for the second value (decimal) then validate that no other character than numbers are inserted with ctype_digit.

However if the price is for exampel 1000 and I leave the decimal field in the form in blank, the validation I do with ctype_digit gives error as the form does not send anything with the field empty.
I have this validation:
PHP Code:
if ( ctype_digit($_POST['price']) &&  ctype_digit($_POST['decimal'])) {
    
$holidaycost $price.".".$decimal

So I tried to convert the empty field with a 0, but canīt find the way.
Maybe this is not the way to go.


I don't think you need two inputs, unless they are for two very different things like price and tax or something. What it sounds like is you're just worried about price, so your form can look like this:

PHP Code:
<form>
<
input type='text' name='price' />
<
input type='submit' name='submit' />
</
form


When your PHP gets the value of "price" sanitize the data, strip out anything that's not a number, you can use regex or a filter function from PHP, or both. Filter Functions

After you sanitize, you'll just have a number, ideally. This is when you make sure that it really is a number, use ctype_digit() to validate it. If you have other validation requirements go through those as well. If validation fails at any point display an error to the user.

After all of that, go with gw1500se's suggestion and use sprintf() to put it in the database.

I hope that I understood what you're trying to do correctly and that this helps.
__________________
"Those who can make you believe absurdities can make you commit atrocities."

Reply With Quote
  #8  
Old January 16th, 2013, 01:46 PM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Quote:
Originally Posted by Jyncka
I don't think you need two inputs,

When your PHP gets the value of "price" sanitize the data, strip out anything that's not a number, you can use regex or a filter function from PHP, or both.

After you sanitize, you'll just have a number, ideally. This is when you make sure that it really is a number, use ctype_digit() to validate it. If you have other validation requirements go through those as well. If validation fails at any point display an error to the user.

After all of that, go with gw1500se's suggestion and use sprintf() to put it in the database.

I hope that I understood what you're trying to do correctly and that this helps.

Yes I think I understand, but if the person write as price 100.50 and I take away the dot with filter or ctype_digit, then I use sprintf to get 2 decimals, wont I get as price 10050.00?
I can see in my bank they only have one field.
The regex I ever seen and the functions, not sure wich to use.

Reply With Quote
  #9  
Old January 16th, 2013, 01:48 PM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Quote:
Originally Posted by gw1500se
I still am not understanding what the 2 fields represent. Are you trying to split your entry into an integer part and a 2nd field for the decimal part? You are making it hard on yourself and your users. Just let them enter a single field that can be a decimal number.


For price 1000.50
the first input for 1000 and the second for the decimal part.

Reply With Quote
  #10  
Old January 16th, 2013, 01:49 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Why are you wanting to take away the decimal point? There is something you are trying to do with these numbers that you are not explaining clearly.

Last edited by gw1500se : January 16th, 2013 at 01:58 PM.

Reply With Quote
  #11  
Old January 16th, 2013, 01:57 PM
Jyncka's Avatar
Jyncka Jyncka is offline
We're trapped inside a game!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Location: Maryland
Posts: 297 Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)Jyncka User rank is Lieutenant General (80000 - 90000 Reputation Level)  Folding Points: 37258 Folding Title: Starter FolderFolding Points: 37258 Folding Title: Starter Folder
Time spent in forums: 5 Days 8 h 35 m 57 sec
Reputation Power: 902
Read how sprintf() works, I would use the "float" type and then tell it that you want a precision of 2.

PHP Code:
echo sprintf('%.2f'$price); 


edit: gw1500se is much faster than me, so I apologize if my replies are confusing.

Reply With Quote
  #12  
Old January 16th, 2013, 01:59 PM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Quote:
Originally Posted by gw1500se
Why are you wanting to take away the decimal point? There is something you are trying to do with these numbers that you are not explaining. clearly.


As she said take away all character not being a number, and the dot in 1.000 is not correct, however this dot is correct in 110.50
these are all correct formats: 100, 1000, 1000.00, 1000.45 etc
however these are incorrect $1000, 1.000, 1,000, 1000,45 etc.

Reply With Quote
  #13  
Old January 16th, 2013, 02:16 PM
newtonperri newtonperri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 34 newtonperri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 21 m 33 sec
Reputation Power: 1
Quote:
Originally Posted by Jyncka
Read how sprintf() works, I would use the "float" type and then tell it that you want a precision of 2.

PHP Code:
echo sprintf('%.2f'$price); 


edit: gw1500se is much faster than me, so I apologize if my replies are confusing.

I like that, however I guess I need some filtering
as 100, 100.50 works perfect.
However 100,50 does not, easy to fix replacion with a dot
but 1.100.50 gives me 1.00

Reply With Quote
  #14  
Old January 16th, 2013, 02:17 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Actually, 1.000 is a proper number. Jyncka has already explained how to get a resulting good number without using separate fields for integers and decimal places.

Reply With Quote
  #15  
Old January 16th, 2013, 11:08 PM
drumn4life0789 drumn4life0789 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 129 drumn4life0789 User rank is Sergeant (500 - 2000 Reputation Level)drumn4life0789 User rank is Sergeant (500 - 2000 Reputation Level)drumn4life0789 User rank is Sergeant (500 - 2000 Reputation Level)drumn4life0789 User rank is Sergeant (500 - 2000 Reputation Level)drumn4life0789 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 20 sec
Reputation Power: 10
I guess I don't understand what is really going on here.

Seems to me that all that needs to be done is strip out anything that is not a number or a decimal. Then check to make sure that there is only one decimal in the string. Then you check that if there is a decimal that there is only two digits behind it.

if we get all that as true we have a valid input. But since it is money I would say you throw it back to the user and ask if this is correct. And format it like so in the display, so that it is easy for the user to read
$###,###.##

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Price in form-input with format 0000.00

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap