chetan mehta
chetan mehta

Reputation: 369

stuck with empty and foreach

<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 />";
}
}
}
?>

The problem is that on leaving all the spaces blank in my formIts only showing the last ement of associative array. For ex:-Leave firstname,lastname ,email, age then it will just show 'Age filed cannot be left blank' Similarly if age is already filled in my input field then it will just show 'Email field can not be left empty' Well I want it to display names of all fields that are left empty

Upvotes: 2

Views: 132

Answers (4)

AKS
AKS

Reputation: 4658

In order to check if a specific key is empty, you must make sure that it's set first (user can edit his HTML source and not send some field, triggering some warnings in your site). Other than that, I find your code a little messy so you will find it difficult to debug or read after a while.

Here, I'm rewriting the PHP part to check the field is entered and is not empty.

<?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.
}
?>

Upvotes: 0

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

I think you're confusing:

foreach($emt as $value=>$variable)
{

with

foreach($emt as $variable=>$value)

(I would name the variable $variable $key instead, but's just a matter of taste)

And the same thing goes for the array which other answers has shown.

$emt = array(key => value, key => value,  ....);

Upvotes: 0

Tamil Selvan C
Tamil Selvan C

Reputation: 20209

Change it as

$emt=array("Firstname"=>$_POST['fname'],"LastName"=>$_POST['lname'],"Email"=>$_POST['email'],"Age"=>$_POST['age']);

Upvotes: 0

bwoebi
bwoebi

Reputation: 23777

You have to change $key and $variable:

$emt=array("Firstname"=>$_POST['fname'],"LastName"=>$_POST['lname'],"Email"=>$_POST['email'],"Age"=>$_POST['age']);

Upvotes: 2

Related Questions