1

I need answer for this type of question. Please I need to get type of variable from HTML input field and to echo that type of $var.

From this form I need to check what type of data is entered. Is it string or integer, etc.

<form>
<input type="text" name="podatak" value="podatak" />
<input type="Submit" name="Submit">
</form>

Thanks in advance.

2
  • You do not know what input/select/textarea type you got. Commented Sep 17, 2012 at 11:20
  • @user1677402 See my edit to the answer, that's probably what I would go with. Commented Sep 17, 2012 at 11:38

6 Answers 6

7

All form submimssions are strings to begin with as such. You can however use functions like is_numeric to see if the item is a number and is_array to see if the form element being passed is an array (ie, checkboxes, possibly files and the like).

Based on your comments, I would do something like the following:

function whatTypeAmI($var)
{
    if(is_array($var)
    {
        // Request is likely a checkbox or multiple files.
        echo "I am an Array! Yeeehaaa!";
    }
    else if(is_numeric($var))
    {
        // I look like a number, or someone 
        // typed in a number into a text field.
        echo "I am a Fluffeh number!";
    }
    else
    {
        echo "Goodness, who knows what on earth happened?";
    }
}

foreach($_POST as $val)
{
    whatTypeAmI($val);
}
Sign up to request clarification or add additional context in comments.

Comments

2

It's always a string or an array. If is_array($_POST['value']) returns true; it's an array. In any other case it's a string.

Comments

0

i guess you are trying to find form field type not the data type... like text, submit, button etc... if that is your scenario then i am afraid there is not such function. As far as i know form post will not send any form type information to the server... any way you can do it using associative array. ex: on your php side do some thing like.

<?php 
     function getType($elementName){
         $formType = array('elementName1' => 'elementType1', 'elementName2' => 'elementType2'......); 
         if(array_key_exists($elementName, $formType))
             return $formType[$elementName];
         return 'Unknown';
     }

     foreach($_POST AS $elementName => $elementVal){
          echo 'name = '.$elementName.' type = '. getType($elementName).' value ='.$elementVal;
     } 

1 Comment

I need a type of the input data, is it a integer or string. I need to enter something in the input field and to echo type of that input data.
0

You can use var_dump($variable).

Comments

-1

you can make use of these functions : is_int , is_array , is_null, is_float , is_double , is_bool, etc.

5 Comments

But will it not always be a string or array?
then, use other methods, such as is_null, is_float, is_double, etc ?
It will still be only a string or an array.
or null if the input field does not exist .
In that case it won't be defined, not null.
-2
function gettype_custom($var){
    if(is_array($var)){
        return 'array';
    }elseif(is_numeric($var)){
        return 'numeric';
    }elseif(is_string($var)){
        return 'string';
    }else{
        return 'unknown';
    }
}

3 Comments

Not my down-vote, but I'm guessing you got Down-voted because $_POST or $_GET data is either an array or a string. gettype is therefore not useful in this case
but how to connect it with this: <form action="page.php" method="get"> <input type="text" name="podatak" value="<?php $var ?>" /> <input type="Submit" name="Submit"> </form>
<input type="text" name="podatak" value="<?php echo $var; ?>" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.