Here's the situation:

I have a simple HTML form that passes user-inputted data to a PHP script for processing. The HTML form consists of the typical <input type=text> fields. One of the fields within that HTML page asks the user for a numerical value. On the PHP script, I want to check to see if the user inputted numbers (integer) into the field. When I do a gettype() check on the variable $year (passed from the HTML form), the result is "string", even if the value entered into the field was a number like 1999. Thus, I cannot validate whether or not the data inputted is a string or an integer.

I know I can convert the submitted data by forcing it to become an integer by using settype(), but is there any reason why all data submitted via web forms are strings?

Any ideas would be of a big help!

Thanks.

    Any GET/POST variable will come in as strings. It's an HTTP limitation. All it gets is the variable name and a value, so of course, it's not going to assume that all variables including your name are integers 😉 Your typical HTTP/1.0 method will give you something like this for a GET:

    GET /page.php?a=123&b=hello&c=a1b2c3d4

    from that alone, the web servers must make sense of what values it must assign the variables. As if that's not bothersome enough, autotype detection would simply be a waste of time. That's mainly why PHP is not strong-typed like... C or Pascal.

    Of course, there are millions of ways of detecting an integer/float from a string. You can use regexp's, str_tr all numbers, etc., etc. I'll leave you to find the best way for what you're seeking. Hope this gives you an idea 😉

      Well, you can certainly check to see if the input is numbers of not

      I suggest to use ereg() function

      Basic validaion is this:
      <b>
      If(!ereg("([0-9]{1,})$", $string))
      {
      echo "<script>alert(\"This form has to have a numeric characters\")</script>";
      }
      </b>

      But, check also eregi() and I know that there are other string functions that would check your strings for certain type of data.

      Di

        Write a Reply...