<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 />";
}
}
}
?>
问题在于,在我的formIts中将所有空格留空时只显示关联数组的最后一个。 例如: - 离开名字,姓氏,电子邮件,年龄,那么它只会显示'年龄归档不能留空' 同样,如果我的输入字段中已经填充了年龄,那么它只会显示“电子邮件字段不能为空” 好吧,我希望它显示所有空白字段的名称
答案 0 :(得分:2)
您必须更改$ key和$ variable:
$emt=array("Firstname"=>$_POST['fname'],"LastName"=>$_POST['lname'],"Email"=>$_POST['email'],"Age"=>$_POST['age']);
答案 1 :(得分:0)
将其更改为
$emt=array("Firstname"=>$_POST['fname'],"LastName"=>$_POST['lname'],"Email"=>$_POST['email'],"Age"=>$_POST['age']);
答案 2 :(得分:0)
我觉得你很困惑:
foreach($emt as $value=>$variable)
{
与
foreach($emt as $variable=>$value)
(我会将变量命名为$ variable $ key,但这只是一个品味问题)
对于其他答案所显示的阵列也是如此。
$emt = array(key => value, key => value, ....);
答案 3 :(得分:0)
为了检查特定键是否为空,您必须确保首先设置它(用户可以编辑他的HTML源,而不是发送一些字段,在您的站点中触发一些警告)。 除此之外,我发现你的代码有点乱,所以你会发现很难在一段时间后调试或阅读。
在这里,我正在重写PHP部分以检查输入的字段是否为空。
<?php
$required = array(
'fname' => 'First name',
'lname' => 'Last name',
'email' => 'Email address',
'age' => 'Age',
);
$errors = array(); // Here, we store all the error messages. If it's empty, we are good to go.
foreach ($required as $key => $label) {
if (isset($_POST[$key]) || empty($_POST[$key])) {
$errors[] = "$label field is required.";
}
}
if (!isset($_FILES["file"])) {
$errors[] = 'Please upload an image';
}
elseif (isset($_FILES['file']['error']) && $_FILES['file']['error']) {
$errors[] = 'An error occurd while uploading your photo';
}
if ($errors) {
print '<ul>';
foreach ($errors as $error) {
print "<li>$error</li>";
}
print '</ul>'
}
else {
// All fields are filled and not empty, file is uploaded successfully. Process your form.
}
?>