开发者

PHP test for an empty form element doesn't work

I have two elements on my form - textarea and file eleme开发者_JAVA百科nt for file uploads. I need one of the two not to be empty in order for the form to get submitted. Here is how I check it:

<?php

$text = $_POST['text'];
$uploadedfile = $_FILES['uploadedfile'];    
if (isset($_POST['submit'])) {

        if((empty($_POST['text'])) && (empty($_FILES['uploadedfile']))) {
                $errors .= 'Please either enter your text or attach a file.<br/><br/>';
            }

        }
if (!$errors) { do some code...
              header("Location: http://mysite.com/mysite/submitted.html");
} 
else {
            echo '<div style="color: red; font-weight: bold; text-align: center"> The form was not sent. Some data is missing:<br />' . $errors . '<br/>
                    </div>';



        }
    }

?>

Here is my HTML:

    <form id="form" action="quot_form.php" method="post" enctype="multipart/form-data">
<p>
            <textarea rows="15" cols="50" name="text"><?php echo $text?></textarea>
        </p>
        <p class = "upload">        
            <input type="file" class="file" name="uploadedfile" />
        </p>
    </form>

Any idea why the form gets submitted even with both fields empty and no error message gets echoed? Thank you!


You're using a logical AND (&&) instead of a logical OR (||).

if((empty($_POST['text'])) || (empty($_FILES['uploadedfile']))) 

Added: Looks like the files array is problematic to check in this manner. Try this:

if((empty($_POST['text'])) && ($_FILES['uploadedfile']['size'] < 1)) 


I would rewrite it as follows

$test = $_POST['test'];
if(empty($test, $uploadfile) {
//Do Something
    }


The link at the bottom explains much better but the jest of it is:

empty() cannot be used with string variables for the simple fact that empty() returns true if your variable is set the the string value '0'. If the string character '0' is a possibly valid value for your string variable — if all non-zero-length strings are valid — you cannot use empty(). This is even more dangerous if you are using empty() to check if a string variable is defined. Also worth mentioning, empty() returns false for a string value that is nothing but a single space (or any number of spaces really).

you cannot simply replace the use of empty() with a conditional that checks if the string variable is equal to null or has a string length of zero. If your string variable happens to be undefined, empty() will quietly return true whereas strlen($mystring) and $mystring == null will throw PHP warnings. If your variable may possibly be undefined, then you have to first check of the variable is defined using isset(). After that you can check if the string is null.

Answer can be found here: http://www.zachstronaut.com/posts/2009/02/09/careful-with-php-empty.html


You could always use this as the tester if (empty($_POST["text"]) ^ empty($_FILES['uploadedfile'])) It use an xor, which tests if either is set to true, but not both, or neither.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜