jmcc Posted October 6, 2009 Share Posted October 6, 2009 Hi I'm using JavaScript to add commas to a price field in my website, then php strip commas to remove them before feeding the database. The field in the database is set to int. Every time the value is fed into the db only the digits up to the comma is inserted not the all the digits. Example: 100,000 is fed as 100 and 10,000 is fed as 10 I believe its because the ',' is converted to a ''(space) and then the browser thinks its the end of the value entered. Here's the code: <input type="text" name="price" id="price" onkeyup="format(this)" onchange="format(this)" onblur="if(this.value.indexOf('.')==-1)this.value=this.value" value="<?>php echo str_replace("," ,"",($row_rsseller['price'])); <?>" size="32" /> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/ Share on other sites More sharing options...
Adam Posted October 6, 2009 Share Posted October 6, 2009 I assume you're using intval? You can use regex to better remove any non-numeric digits like this: $price = preg_replace('/[^0-9]/', '', $price); Read up more on intval() to understand why it was parsing the integer like it was. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931427 Share on other sites More sharing options...
jmcc Posted October 6, 2009 Author Share Posted October 6, 2009 Thank you for the reply, but it still drops digits. code: <input type="text" name="price" id="price" onkeyup="format(this)" onchange="format(this)" onblur="if(this.value.indexOf('.')==-1)this.value=this.value" value="<?php preg_replace('/[^0-9]/', '', ($row_rsseller['price'])); ?>" size="32" is there a php method that can add commas to my filed as user types Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931442 Share on other sites More sharing options...
jmcc Posted October 6, 2009 Author Share Posted October 6, 2009 I just found that if there are 2 commas in the price then the second comma doesnt get stripped. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931445 Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 I'm confused. The use of... echo str_replace("," ,"",($row_rsseller['price'])); ... seems to me to indicate you are removing commas from the item before outputting it to the form, this has nothing to do with adding it to the database. The use of str_replace() (in the exact format you have) should work, providing you are doing it to the value your inserting into the database. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931446 Share on other sites More sharing options...
Adam Posted October 6, 2009 Share Posted October 6, 2009 As cags has said, the problem is with how you enter the data. You're either using intval() and / or trying to enter the string into an integer field. The str_replace() method will work, but only for commas. White space, dots and any other characters that are in there on the other hand would still give you the same problem. The reg exp method I showed you before would prevent any of these messing up the data. Thinking about it though, why are you storing prices in an integer field? Prices surely must contain a "." to separate units? Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931451 Share on other sites More sharing options...
mrMarcus Posted October 6, 2009 Share Posted October 6, 2009 As cags has said, the problem is with how you enter the data. You're either using intval() and / or trying to enter the string into an integer field. The str_replace() method will work, but only for commas. White space, dots and any other characters that are in there on the other hand would still give you the same problem. The reg exp method I showed you before would prevent any of these messing up the data. Thinking about it though, why are you storing prices in an integer field? Prices surely must contain a "." to separate units? check your db field structure. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931469 Share on other sites More sharing options...
jmcc Posted October 6, 2009 Author Share Posted October 6, 2009 The price i am feeding into my db is property pricing, which do not user decimals. I made it field integer so that I may sort the pricing from less to most expensive. doesn't the str_replace() method turn the comma into white space? I am using JS to convert the price to a price with commas while the user is typing, the before I insert the price to db i call the str_replace method, but the comma is turned into white space and the number is ended at first white space. 10000 is turnd into 10,000 is turned into 10 000 is fed to db as 10 Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931470 Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 str_replace("," ," ",($row_rsseller['price'])); Will put a space (whitespace) in your string, causing problems, whereas... str_replace("," ,"",($row_rsseller['price'])); ...will replace the comma with nothing so it should work fine. If you were having problems it's more than likely the way your using the function not the function thats at fault. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931480 Share on other sites More sharing options...
Adam Posted October 6, 2009 Share Posted October 6, 2009 Firstly " " is not an integer. Secondly str_replace will replace the matched string with whatever is in the second parameter. If you're using JS to add commas to the input, then replacing the commas in the DEFAULT value of the input isn't what you need to do. You need to apply it before it's inserted into the database. All you are doing in your examples is applying the str_replace / preg_replace functions to the data you have just taken from the database, then modifying it again and trying to insert a string into an integer field. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931482 Share on other sites More sharing options...
jmcc Posted October 6, 2009 Author Share Posted October 6, 2009 Yes I believe so I am trying to get entered value, add commas, remove commas in one statement. I'll try stripping commas before db parse. thanks for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931490 Share on other sites More sharing options...
Adam Posted October 6, 2009 Share Posted October 6, 2009 Think logically about that, add the commas then remove them in the same statement? Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931493 Share on other sites More sharing options...
jmcc Posted October 6, 2009 Author Share Posted October 6, 2009 well it kinda works. haha add, strip and assign yeah yeah i know Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931496 Share on other sites More sharing options...
mrMarcus Posted October 6, 2009 Share Posted October 6, 2009 why not instead of javascript to add a comma, just use number_format() on the initial value, where your inital value (number) is an integer. $num = (int)100000; //where 100000 is whatever number you're working with; $new_num = number_format ($num); //assigns comma to $num -> 100,000 or am i completely missing something? this way, $num gets inserted into the db, and $new_num gets outputted to the page. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931724 Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 I would use number_format for the output, and then not even worry about stripping it xD However, str_replace would work fine right before the DB entry. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931907 Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 well since he's using javascript to dynamically add the commas, a PHP function won't really work in his case (i'm assuming, this whole post kind of confuses me) can you post some more code that the 3 lines you have? post the actual form processing code Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931922 Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 I guess I can't think of a reason he's using Javascript xD Assuming you HAVE to use Javascript tho, I still think a simple str_replace before you put the data into the DB will suffice just fine! Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931929 Share on other sites More sharing options...
cags Posted October 6, 2009 Share Posted October 6, 2009 I must admit I was originally under the impression that the whole reason the OP was using javascript was because they were dynamically adding the commas as the user inputs data. For example as soon as you enter a 4th char it adds a comma after the first etc. But the more the thread goes on the more confusing it seems to get. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931936 Share on other sites More sharing options...
mikesta707 Posted October 6, 2009 Share Posted October 6, 2009 well he has to use it for his input text box to dynamically add commas to what his user is typing, or this is what i could ascertain from his description of the problem he has Or maybe not. who knows any more Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-931937 Share on other sites More sharing options...
jmcc Posted October 7, 2009 Author Share Posted October 7, 2009 can num_format() change the number while the user is entering it? Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-932531 Share on other sites More sharing options...
mrMarcus Posted October 7, 2009 Share Posted October 7, 2009 can num_format() change the number while the user is entering it? in an "on-the-fly" manner? meaning, an AJAX type action? if so, yes. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-932532 Share on other sites More sharing options...
Adam Posted October 7, 2009 Share Posted October 7, 2009 If you want it that bad, you could use JavaScript equivalent for number_format() .. But I think some simple formatting would be the better option. Quote Link to comment https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/#findComment-932539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.