Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/176671-comma-stripping-problem/
Share on other sites

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

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.

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?

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.

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

 

 

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.

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.

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.

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.