Missing Zeros when posting html form data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ov3rfl0w
Forum Newbie
Posts: 2
Joined: Mon Feb 26, 2007 6:04 am

Missing Zeros when posting html form data

Post by ov3rfl0w »

Hi

Usually when I have a problem coding it lasts for a few hours or until I find a fix somewhere on the net but I have been stuck on this problem for about 2 days and I cannot find anything relavent on the internet.

When I submit a form (form.php) to a processing page (add.php) any text with more than one zero at the end of the string, has the zeros eliminated. The main text box that I am using will take a number but this also happens in a textbox that I want to take text only [a-z0-9]

e.g.

text field = 1 => output after post = 1
text field = 10 => output after post = 10
text field = 100 => output after post = 1
text field = t10 => output after post = t10
text field = t100 => output after post = t1
text field = t100t => output after post = t100t
text field = 220.00 => output 22
text field = 220.02 => output 220.02

As you can see the problem occurs if the last characters of the textbox are 00 and the program will remove all trailing zeros from the string. The same problem occurs if the form type='number'.

I have ran loads of tests to establish if there is an error in my code but it happens as soon as aVar[$i]=$_POST['aTextbox']; is executed
the value of aVar[$i] is as shown on the ouput details above

I can paste in some of the code if you like but from what I can tell, it's a general problem

Apologies if this is a silly question and i'm missing something basic.

Thanks

Tim
Last edited by ov3rfl0w on Mon Feb 26, 2007 6:55 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

PHP is converting it to a number. You will need to use str_pad() or something similar to add those figures back on.
ov3rfl0w
Forum Newbie
Posts: 2
Joined: Mon Feb 26, 2007 6:04 am

Post by ov3rfl0w »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Thanks for the reply...

I've been messing with it and here's the solution I came up with:

Code: Select all

$i=0;
while(sizeof($columns)-1 >= $i){ //the amount of columns the form is submitting
	if(strlen($_POST[$columns[$i]])!=strlen($columnsContent[$i])){ //if the pre posted string length is not the same as the post posted data
		$columnsContent[$i]=str_pad($columnsContent[$i],strlen($_POST[$columns[$i]]),"0",STR_PAD_RIGHT); //add the zeros on the end until the string length matches the length	
	}
	if($addQuotes[$i]==true){ //if the item requires quotes to be accepted in sql
		$columnsContent[$i] = "'".$columnsContent[$i]."'"; //add the quotes
	}
	$aSQLMiddle = $aSQLMiddle.",".$columnsContent[$i]; //construct the sql
	$i++; //next form field
}


It needs a bit of fiddling to deal with decimals but it shouldn't be a problem.

Thanks for the nudge in the right direction

Much appreciated

Tim


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post by visitor-Q »

that seems like too much work to 'get around' the problem. you should just fix the problem at the source. a problem you may be having could be located in your SQL database. what are the field-types? CHAR, VARCHAR, INT?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you post the html of the form you are using to test that problem?
Post Reply