PHP中提交表单期间的igoring条件
我试图在我的提交表格中加入一个条件。我想检查我的任何字段中没有任何空值。在我的表格中,我有两个文本区和一个文件上传区 我提出了如下条件PHP中提交表单期间的igoring条件,php,Php,我试图在我的提交表格中加入一个条件。我想检查我的任何字段中没有任何空值。在我的表格中,我有两个文本区和一个文件上传区 我提出了如下条件 if (($_POST['question'] != "") AND ($_POST['answer'] != "") AND ($_FILES['picture_name']['name'] != "")) { echo "ok"; } else { echo "field empty"; } 如果文件上载或问题为空,但其accept和echo ok甚至答案
if (($_POST['question'] != "") AND ($_POST['answer'] != "") AND ($_FILES['picture_name']['name'] != "")) {
echo "ok";
}
else {
echo "field empty";
}
如果文件上载或问题为空,但其accept和echo ok甚至答案为空,则其给出错误。如果我的状况有任何问题,请告诉我。
谢谢这可能会有帮助
if (!empty($_POST['question']) && !empty($_POST['answer']) && is_uploaded_file($_FILES['myfile']['tmp_name'])) {
echo "ok";
}
else {
echo "field empty";
}
我发现最好在继续处理代码之前检查并消除所有问题。希望这能让你走上正确的轨道,知道什么事情可能会发生,什么事情可能不会发生
<?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;
你能解释一下为什么要使用!=“”
失败?empty()
函数优于!=“”因为(null、false、0、空字符串)不等于“”是。isset()、empty()是很好的搭配。请阅读。。。我认为应该使用的是isset()
:您是否也可以发布导致调用此PHP代码的相应HTML代码?您好!很抱歉我的文本区域中有空格,所以我们无法检测到它是空的。所以基本上你在结尾或开头有一些额外的空格,这导致了问题?如果是这样,那么您应该在检查之前修剪字符串。参见链接