tags:

views:

93

answers:

3

Why won't this print "success" when I submit the form? I'm pretty sure it should.

<?php
    if (count($_POST) > 0) {
        echo "success!!";
    }
?>

<form method="post" enctype="multipart/form-data">
    <input type="file" name="userfile" />
    <input type="submit" value="upload" />
</form>
+6  A: 

At a guess, the submit field has no name, so it won't be included in $_POST. Your file upload will be placed in $_FILES, see Handling file uploads.

rjh
+3  A: 

It is also good practice to NOT ommit the action attribute.

If you want the form to submit to itself, try

<form method="post" action="?" enctype="multipart/form-data">

or

<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>" enctype="multipart/form-data">

Further reading on second method Disclaimer: Link to my own blog

alex
Why is that a disclaimer? You're not sure about the contents of your blog?
soulmerge
Just to let people know that the link is to my own blog so I'm biased by linking to it :P
alex
A: 
<?php
    if (count($_POST['submit']) > 0) {
        echo "success!!";
    }
?>

<form method="post" enctype="multipart/form-data" action="">
    <input type="file" name="userfile" />
    <input type="submit" value="upload" name="submit"/>
</form>
veb
How about `if ($_POST['submit'] == 'upload')`
rjh
I normally just have if ($_POST['submit']) {
veb