<form action="http:\\127.0.0.1\rechecking.php" method="post" enctype="multipart/form- data"><pre>
Enter your first Name: <input type="text" name="fname" size="15"></input>
Enter your Last Name: <input type="text" name="lname" size="15"></input>
Your Email Id: <input type="text" name="email" size="15"></input>
Your age: <input type="text" name="age" size="1"></input>
Upload your Image: <input type="file" name="file"></input>
<input type="Submit" value="Submit"></input></pre>
</form>
<?php
if(!empty($_POST["fname"])&&!empty($_POST["lname"])&&!empty($_POST["email"])&&!empty($_POST["age"]))
{
if($_FILES["file"]["error"]>0)
{
echo $_FILES['file']['error'] ."error is there in uploading files";
}
}
else
{ $emt=array($_POST['fname']=>"Firstname",$_POST['lname']=>"LastName",$_POST['email']=>"Email",$_POST['age']=>"Age");
foreach($emt as $value=>$variable)
{
if(empty($value))
{
echo $variable." cannot be left blank<br />";
}
}
}
?>
问题是,在我的表单中将所有空格留空时,它只显示关联数组的最后一个元素。 例如:-留下名字、姓氏、电子邮件、年龄,然后它只会显示“年龄字段不能留空” 同样,如果我的输入字段中已经填写了年龄,那么它只会显示“电子邮件字段不能留空” 好吧,我希望它显示所有留空字段的名称
最佳答案
您必须更改 $key 和 $variable:
$emt=array("Firstname"=>$_POST['fname'],"LastName"=>$_POST['lname'],"Email"=>$_POST['email'],"Age"=>$_POST['age']);
关于php - 陷入空和 foreach 困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15910908/