Discuss Price in form-input with format 0000.00 in the PHP Development forum on Dev Shed. Price in form-input with format 0000.00 PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 34
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.
Posts: 2,886
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.
Posts: 34
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.
Posts: 2,886
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.
Posts: 297
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:
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."
Posts: 34
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.
Posts: 34
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.
Posts: 34
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.
Posts: 34
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
Posts: 2,886
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.
Posts: 129
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
$###,###.##