Jump to content

Getting a datatype from a form submission (one user input only)


j.smith1981

Recommended Posts

I was wondering just a general question here.

 

I am just going through a text book on the main aspects of a problem solving approach to PHP, but when I was just trying out one of my own theories on this particular one of my own:

 

<html>
<body>

<h1>User Input set as functions</h1>

<tt>Please enter a value below:</tt>
<br>
<form id="userInput" name="userInput" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">

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

<input type="submit" value="Send This" />

</form>

<?php

function mistake($errorvalue) {
    echo $errorvalue;
}

function getValue($userInput) {
    return $userInput;
}

if(!isset($_GET['input']) || $_GET['input'] == '') {
    
    mistake("No value received yet!"); // calls the error function
    
} else {
    $input = $_GET['input'];
    echo "You did enter something, this was: ";
    
    $userInput = getValue($input);
    echo "$userInput";
}
?>

</body>
</html>

 

I am quite impressed with what I have done there, though I know its nothing special and could obviously be done not using functions at all, just wanted to see if I could get one that returns something, in this case the 'getValue()' function.

 

But when I've set it to work out what type of variable ie gettype is it? (going off completely memory here), its always a string, even if all I do is enter a 1, even tried not using the GET method for the form and used the POST one instead it still says that a single integer is a string.

 

Why just out of question is it doing this?

 

Just quite interested thats all.

 

Thanks for your time and I look forward to any replies,

Jeremy.

By definition (it's mentioned somewhere in the php.net documentation) all $_GET, $_POST, and $_COOKIE variables are string data types, no matter what they contain.

 

You should instead be more concerned with the value in the variable, than the type of the variable. See the is_numeric function to determine if the value in a variable is numeric.

By definition (it's mentioned somewhere in the php.net documentation) all $_GET, $_POST, and $_COOKIE variables are string data types, no matter what they contain.

 

You should instead be more concerned with the value in the variable, than the type of the variable. See the is_numeric function to determine if the value in a variable is numeric.

 

Ah ok thats cool, just not really using this of course for productional purposes more a prove to myself I could get it to return a value that's all, for educational purposes myself.

 

Thanks for your help though as always much appreciated!

 

Jeremy

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.