954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Form Data Sent As String Rather Than Integer

Hey guys hopefully this is just a quick question.

I've a form that is sending data to a PHP script. The form has the following textbox:

<input id="quantity1" name="quantity1" type="text" value="" />


Whenever it is posted I do the following:

function isInteger($val) {
  if (is_int($val)) {
    return true;
  } else {
    return false;
  }
}

$test = $_POST["quantity1"];

echo gettype($test); // Which is returning as a string

  if (  isInteger($test)  ) {
    echo "<p>It's an integer!</p>";
  } else {
    echo "<p>It's not an integer!</p>";
  }


Even if I type a number such as '1' or '123' into the textbox, it's always being picked up by the script as string type. Is there anyway to convert this into an integer, or even hardcoding the $test variable to be strictly an integer?

Thanks in advance,

Anthony

antwan1986
Junior Poster
110 posts since May 2008
Reputation Points: 14
Solved Threads: 8
 

try is_numeric() instead... is_int is a really specific function that only works when calling it like

is_int(1); //true
is_int('1'); //false
kevindougans
Junior Poster
129 posts since Oct 2007
Reputation Points: 13
Solved Threads: 7
 

Thats great man, the is_numeric() function works great, although if I run a gettype() on the variable, it still appears as a string.

So really the is_numeric() function is looking for a string that contains numbers right?

antwan1986
Junior Poster
110 posts since May 2008
Reputation Points: 14
Solved Threads: 8
 

Almost... the is_numeric checks the variable you passed to it is a number - of any kind. If you pass "nvg8vn4g4g444" to it, it should return false...

kevindougans
Junior Poster
129 posts since Oct 2007
Reputation Points: 13
Solved Threads: 7
 

Yep your right, thats returning false for me. The function is working perfectly now, thanks very much for all your help. I've added to your rep and this is marked as solved!

Anthony

antwan1986
Junior Poster
110 posts since May 2008
Reputation Points: 14
Solved Threads: 8
 

:)

Anytime mate; I owe it to this board for the amount of times it's helped me out over the past year.

kevindougans
Junior Poster
129 posts since Oct 2007
Reputation Points: 13
Solved Threads: 7
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You