Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP upload - Why isset($_POST['submit']) is always FALSE

Tags:

php

I have the following code sample upload3.php:

<html>
<head>
<title>PHP Form Upload</title>
</head>
<body>

<form method='post' action='upload3.php' enctype='multipart/form-data'>
    Select a File:
    <input type='file' name='filename' size='10' />
    <input type='submit' value='Upload' />
</form>

<?php

if (isset($_POST['submit']))
{
    echo "isset submit";
}
else 
{
    echo "NOT isset submit";
}

?>

</body>
</html>

The code always returns "NOT isset submit". Why does this happen? Because the same script upload3.php calls itself?

like image 474
a2011 Avatar asked Jul 26 '10 18:07

a2011


People also ask

What is if isset ($_ POST?

Definition and Usage. The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

Is Isset submit?

Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST['submit'] method. Remember in place of submit define the name of submit button. After clicking on submit button this action will work as POST method.

Is $_ POST always set?

to check if your script was POSTed. If additional data was passed, $_POST will not be empty, otherwise it will. You can use empty method to check if it contains data. Since $_POST always exists if ($_POST) will do just fine, no need for empty .


2 Answers

You do not have your submit button named:
Change

<input type='submit' value='Upload' />

To:

<input type='submit' value='Upload' name="submit"/>
like image 151
ashurexm Avatar answered Nov 07 '22 11:11

ashurexm


Two things:

You'll want to try array_key_exists instead of isset when using arrays. PHP can have some hinky behavior when using isset on an array element.

http://www.php.net/manual/en/function.array-key-exists.php

if (array_key_exists('submit', $_POST)) { }

Second, you need a name attribute on your button ( "name='submit'" )

like image 44
SomeGuy1989 Avatar answered Nov 07 '22 10:11

SomeGuy1989