I have two elements on my form – textarea and file element 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 (||).
Added: Looks like the files array is problematic to check in this manner. Try this: