tags:

views:

133

answers:

4

Hello all,

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?

Thank you

+3  A: 

Because you don't have any form element whose name property is submit.

Try to use var_dump($_POST) to see the keys that are defined.

Notice that files are an exception; they're not included in $_POST; they're stored in the filesystem and they're metadata (location, name, etc) is in the $_FILES superglobal.

Artefacto
+12  A: 

You do not have your submit button named:
Change

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

To:

<input type='submit' value='Upload' name="submit"/>
manyxcxi
+2  A: 

Try looking at the REQUEST_METHOD and see if it's POST. It's a little bit nicer.

CharlesLeaf
The problem with that method, it's that you don't know which input made the action. If you have many submit button on the same page you won't know which one the user clicked.
HoLyVieR
True. Well if you have those buttons in separate forms, but I myself am not a fan of having multiple forms submit to the same page (different form usually has a different action). Two differently named submit buttons in the same form will both be send in the POST request.
CharlesLeaf
@CharlesLeaf, The submit button data will only be send if it's the control that launch the submit.
HoLyVieR
+3  A: 

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'" )

Michael Mac McCann