Page 1 of 1

int is not an int!

Posted: Fri Apr 27, 2007 3:12 pm
by slash_gnr3k
i have an input form with some fields. obviously i dont want people to add strings to text fields.

this is the attribute in question:

Code: Select all

<p><input type = "text" name = "newpreptime" /></p>
when the form is submitted, this happens:

Code: Select all

$newpreptime=$_POST['newpreptime'];
and this is the test

Code: Select all

					if (is_int($newpreptime))
					{

						echo"this is a int";
					}
	
					else
					{
						echo"this is not a int";
					}
for some reason if i type in an integer or a string, this test always fails and the echo statement fires saying it is not an int. i thought php automatically converted strings to int if valid?
can anyone help?
thanks

Posted: Fri Apr 27, 2007 3:20 pm
by John Cartwright
var_dump($newpreptime) would reveal it actually is not an int, but a string ;)

your looking for is_numeric()

Posted: Sat Apr 28, 2007 4:55 am
by Mohit_Prog
any request coming from a textbox ias always referred as a string.
You have to manually check with some PHP functions or type cast into int.

otherwise you can put validation when you are filling form that only numbers should be entered.
and on the next page typecast it into an int. so simple....