Array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sisleysusie
Forum Newbie
Posts: 16
Joined: Wed Jan 14, 2004 9:44 pm

Array problem

Post by sisleysusie »

Hi all,

I am not sure about the code that i have written below. A newbie here so i suspect there is something wrong with my coding. Can anyone tell me? I have this form that will need certain fields to be filled and i want to use array to check whether the required fields are filled or not. I will use function filled_out for the checking. But not sure how to write the array for the required fields. I include the code for the array and also the function. I will really appreciate your help. Thank you in advance.

Code: Select all

<?php



function filled_out($required)
{
  // test that each variables has a value
  foreach ($required as $key =>$value)
  {   	if (!isset($key) || ($value == ''))
		return false;
   }
   return true;
}


$required = array( 1=>$Courses_Registered, $FirstName, $LastName, $Salutations, $Gender, $Citizenship, $DOB, $Street, $Building, $City, $Country, $Zip, $Educational_Level, $Title, $Primary_Responsibility, $Management_Level, $Company_Name, $Business_Tel, $Fax, $Email,
$Industry, $Employees_Number, $Annual_Sales, $Business_Street, $Business_Street, $Business_Building, $Business_City, $Business_Country, $Business_Zip, $How_Heard);


//check forms filled in
	   if (!filled_out($required))
	{
		header("Location:missing.html");
	}




?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It's a bit easier if you make the required array contain the same values as the form fields. Here's a quick example :

Code: Select all

<?php
if(!empty($_POST['submit'])){
  $required = array(
    'fname', 'lname', 'email'
  );
  $missing = array();
  foreach($required as $req){
    if(empty($_POST[$req])){
      $missing[] = $req;
    }
  }
  var_dump($missing);
}
?>
<form method="post" action="">
<input type="text" name="fname" value="" size="20" maxlength="20" />
<br />
<input type="text" name="lname" value="" size="20" maxlength="20" />
<br />
<input type="text" name="email" value="" size="20" maxlength="20" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
sisleysusie
Forum Newbie
Posts: 16
Joined: Wed Jan 14, 2004 9:44 pm

Post by sisleysusie »

Thanks Mark, but i just dont understand your code maybe it's too hard for me. So, the required fields are fname, lname and email, right? how about missing=array()? What's the contain of it? If $required is empty, $missing[] = $req; means if fname,lname and email are empty, $missing[] = fname, lname, and email, huh? And what will var_dump($missing) do? In my code, if there is any unfilled field, page missing.html will be displayed. How can i do that? Thank you in advance. It's better to be a fool and ask but to be a fool who is afraid to ask, ehh?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

No problem, i should have added some comments ;)
Ok, the $required array contains the fields that are required, the values in there (fname, lname, email) match the names of the form fields (name="fname", name="lname" and name="email" in the form).

When you post the form it loops through the required array and makes sure each of them is present in the $_POST array, in other words it makes sure the required ones have been posted. If they havn't then it adds the field name to the $missing array.

The var_dump() just dumps out the array of missing fields, you should handle it 'more nicely' maybe by doing something like :
if(!empty($missing)){
foreach($missing as $miss){
echo $miss.' must be filled in<br />';
}
} else {
//everything that was required was filed in, so do what you want to do here
}

This loop first checks if there are any fields missing (if(!empty ... ) and displays those that were not filled in.
Post Reply