我有兩個元素在我的表單 - textarea和文件元素的文件上傳。 我需要兩個中的一個不能爲空,才能提交表單。 這是我如何檢查:一個空的表單元素的PHP測試不起作用
<?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>';
}
}
?>
這裏是我的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>
任何想法,爲什麼形式變得更加有兩個字段爲空,並沒有錯誤信息被提交呼應? 謝謝!
的,如果是有關該錯誤的,不正確的行爲。錯誤是兩個都是空的,所以AND是正確的運算符。 – 2011-05-21 02:42:35
哎呦。那麼,他們中的一個明顯地返回了錯誤。 textarea很可能在其中有一些(空的空間)。 – 2011-05-21 02:48:13
或不,基於$ _FILES數組的工作方式。查看更新的答案。 – 2011-05-21 02:52:21