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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.