开发者

Why wont this simple code work all of a sudden?

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

<?php
    if (count($_POST) > 0) {
开发者_StackOverflow中文版        echo "success!!";
    }
?>

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


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.


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


<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜