Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    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
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    970
    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."
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  20. #11
  21. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    970
    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.
    "Those who can make you believe absurdities can make you commit atrocities."
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    73
    Rep Power
    0
    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
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    129
    Rep Power
    11
    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
    $###,###.##
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo