我有两个元素在我的表单 - 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