我正在尝试在表单提交中添加一个条件。我想检查我的任何档案中是否没有任何空值。在我的表单中,我有两个文本区域和一个文件上传部分。
我已经提出了以下条件
if (($_POST['question'] != "") AND ($_POST['answer'] != "") AND ($_FILES['picture_name']['name'] != "")) {
echo "ok";
}
else {
echo "field empty";
}
如果文件上传或问题为空,则给出错误,但即使答案为空,其接受和回显也可以。让我知道我的状况是否有问题。 谢谢
答案 0 :(得分:1)
这可能会帮助...
if (!empty($_POST['question']) && !empty($_POST['answer']) && is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo "ok";
}
else {
echo "field empty";
}
答案 1 :(得分:0)
我发现最好在继续处理代码之前检查并消除所有问题。希望这能使您正确地了解计划的发展方向。
<?php
$problems = array();
// Check the obvious first.
if (empty($_POST) || empty($_FILES)) {
if (empty($_POST)) {
$problems[] = 'POST empty';
}
if (empty($_FILES)) {
$problems[] = 'FILES empty';
}
}
// If those tests passed, proceed to check other details
else {
// Check if the array keys are set
if (!isset($_POST['question']) || !isset($_POST['answer'])) {
if (!isset($_POST['question'])) {
$problems[] = 'Question not set';
}
if (!isset($_POST['answer'])) {
$problems[] = 'Answer not set';
}
}
else {
// If those tests passed, check if the values are an empty string.
if ($_POST['question'] == "" || $_POST['answer'] == "") {
if ($_POST['question'] == "") {
$problems[] = 'Question empty';
}
if ($_POST['answer'] == "") {
$problems[] = 'Answer empty';
}
}
}
// There are many ways to eliminate problems... The next few lines
// are slightly different since they use elseif conditions (meaning
// only one of them will be displayed, if any).
if (!isset($_FILES['picture_name'])) {
$problems[] = 'Picture name not set';
}
elseif (empty($_FILES['picture_name'])) {
$problems[] = 'Picture name empty';
}
elseif (!isset($_FILES['picture_name']['name'])) {
$problems[] = 'Picture filename not set';
}
elseif (empty($_FILES['picture_name']['name'])) {
$problems[] = 'Picture filename empty';
}
}
// If $problems array is still empty, everything should be OK
if (empty($problems)) {
$outcome = 'OK';
}
// If $problems array has values, glue them together and display
// each one on a new line
else {
$outcome = implode(PHP_EOL, $problems);
}
echo $outcome;