PHP条件:忽略输入数组的第一个测试



我想用条件(if, else…)检查输入的输入,到目前为止还不是很复杂。但问题是第一个条件从来没有被考虑过。

下面是我的表单代码:
<form method="POST" action="">
<table>
<caption>author :</caption>
<?php
for($i=1; $i <= $author; $i++){?>
<tr>
<td align="right">
<label for="<?= 'author'.$i ?>"> <?='author '.$i ?> :</label>
</td>
<td>
<input type="text" placeholder=" nom du author" id=" <?= 'author'.$i ?>" name="authors[]">
</td>
</tr>
<?php 
}?>
</table>
<table>
<caption>title :</caption>
<?php 
for($i=1; $i <= $title; $i++){?>
<tr>
<td align="right">
<label for="<?= 'title'.$i ?>"> <?= 'title '.$i ?> :</label>
</td>
<td>
<input type="text" placeholder=" title" id="<?= 'title'.$i ?>" name="titles[]">
</td>
</tr>
<?php 
}?>
</table>
<table>
<caption>article :</caption>
<?php
for($i=1; $i <= $article; $i++){?>
<tr>
<td align="right">
<label for="<?= 'article'.$i ?>"> <?= 'article '.$i ?> :</label>
</td>
<td>
<input type="text" placeholder="ex: score possible(min:0)" id="<?= 'article'.$i ?>" name="articles[]">
</td>
</tr>
<tr>
<td>
<input type="hidden" name="article" value="0">
</td>
<td align="right">
<br />
<input type="submit" name="btnSusbmit" class="modal-btn modal-eventTrigger modal-reportEventTrigger" value="Next">
</td>
</tr>
</table>
</form>
下面是我的PHP代码:
if(isset($_POST['btnSusbmit']))
{
if(!empty($_POST['authors']))
{
$_SESSION['authors'] = array_unique($_POST['authors']);
if(!empty($_POST['titles']))
{
$_SESSION['titles'] = array_unique($_POST['titles']);
if(!empty($_POST['articles']) and !empty($articles) and $articles != 0)
{
$_SESSION['articles'] = array_unique($_POST['articles']);
}
elseif(!empty($articles) and $articles != 0 and empty($_POST['articles']))
{
$error = 'all fields "articles" must be filled in.';
}
}
else 
{
$error = 'all fields "titles" must be filled in.';
}
}
else 
{
$error = 'all fields "authors" must be filled in';
}
}

我改变了测试条件,无论什么条件都是同样的问题。

编辑:

var_dump($POST] = array(5) { ["authors"]=> array(1) { [0]=> string(0) "" } ["titles"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["articles"]=> array(2) { [0]=> string(0) "" [1]=> string(0) "" } ["article"]=> string(1) "0" ["btnSubmit"]=> string(7) "Next" }

首先,你的表单缺少最后一个for循环的右括号:

<?php
for($i=1; $i <= $article; $i++){?>

你在那里打开它,但不能关闭它。

第二,这是你的第一个条件:
if(isset($_POST['btnSusbmit'])) 

它会检查是否有名为" btnsubmit "的POST字段,你所有的提交都有相同的名称所以如果其中有一个被点击了它的值总是为true

我想你想知道的是为什么这句话:

if(!empty($_POST['authors']))

总是返回真,即使'author'字段是空的,那是因为即使你不填写你的文本输入,表单仍然创建一个数组与空字符串。这是var_dump的结果(我将变量定义为5只是为了获得一些测试字段)

array (size=5)
'authors' => 
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'titles' => 
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'articles' => 
array (size=5)
0 => string '' (length=0)
1 => string '' (length=0)
2 => string '' (length=0)
3 => string '' (length=0)
4 => string '' (length=0)
'article' => string '0' (length=1)
'btnSusbmit' => string 'Next' (length=4)

'authors'永远不会为空,因为即使你不填充文本,它仍然是一个数组,里面有元素,如果你想在文本输入为空时抛出错误,那么你需要检查'authors'数组元素中的字符串

相关内容

最新更新