php optional field code odd error
<p>I am working on a php form, and i made two parts, required field and optional field. The following code works well apart from "email" field. I made email field to optional field but somehow it doesnt work as optional, it works like required field. 
if someone could paste the code and give it a try? </p>

<p>Many thanks for any suggestion. </p>

<pre><code>&lt;?php

// Set required fields
$required_fields = array('phone');

$optional_fields = array('name','email');

// set error messages
$error_messages = array(
    'name' =&gt; 'Please enter your name, letters only',
    'phone' =&gt; 'Please enter a valid phone number',
    'email' =&gt; 'Please enter a valid email address',
);

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();

// check form submittal
if(!empty($_POST)) {

    // Loop into required fields and make sure they match our needs
    foreach($required_fields as $field) {       
        // the field has been submitted?
    //  if(!array_key_exists($field, $_POST)) array_push($validation, $field);

        // check there is information in the field?
        if($_POST[$field] == '') array_push($validation, $field);

                // validate the phone number supplied
        if($field == 'phone') if(!validate_phone_number($_POST[$field])) array_push($validation, $field);

    }

        //starts here for optional validation   
    foreach($optional_fields as $field){
        if(!array_key_exists($field, $_POST)) array_push($validation, $field);

            // validate the name if supplied
    if($field == 'name') if(!validate_name($_POST[$field])) array_push($validation, $field);


    // validate the email address supplied
    if($field == 'email') if(!validate_email($_POST[$field])) array_push($validation, $field);

    }   // basic validation result
    if(count($validation) == 0) {

        $form_complete = TRUE;
    }
}

//functions for validation different fields
function validate_name($phone = FALSE) {
    return (!preg_match('/[^a-zA-Z ]/', $phone))? TRUE : FALSE;
}

function validate_email($email = FALSE) {
    return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function validate_phone_number($phone = FALSE) {
    return (!preg_match('/\D/', $phone))? TRUE : FALSE;
}

?&gt;



&lt;?php if($form_complete === FALSE): ?&gt; 


  &lt;form id="form" name="form"  method="post" action=""&gt;

      &lt;label for="name"&gt;Name:&lt;/label&gt;
      &lt;input class="inter" type="text" name="name" id='name' value="&lt;?php echo isset($_POST['name'])? $_POST['name'] : ''; ?&gt;"/&gt;&lt;?php if(in_array('name', $validation)): ?&gt;&lt;span class="phperror"&gt;&lt;?php echo $error_messages['name']; ?&gt;&lt;/span&gt;&lt;?php endif; ?&gt;
&lt;p&gt;&lt;/p&gt;


      &lt;label for="phone"&gt;Phone:&lt;/label&gt;
      &lt;input  type="text" name="phone" id='phone' value="&lt;?php echo isset($_POST['phone'])? $_POST['phone'] : ''; ?&gt;"/&gt;&lt;?php if(in_array('phone', $validation)): ?&gt;&lt;span class="phperror"&gt;&lt;?php echo $error_messages['phone']; ?&gt;&lt;/span&gt;&lt;?php endif; ?&gt;
&lt;p&gt;&lt;/p&gt;


      &lt;label for="email"&gt;Email:&lt;/label&gt;
      &lt;input type="text" name="email" id='email' value="&lt;?php echo isset($_POST['email'])? $_POST['email'] : ''; ?&gt;"/&gt;&lt;?php if(in_array('email', $validation)): ?&gt;&lt;span class="phperror"&gt;&lt;?php echo $error_messages['email']; ?&gt;&lt;/span&gt;&lt;?php endif; ?&gt;
&lt;p&gt;&lt;/p&gt;
   &lt;input type="submit" value="Send"  /&gt;
  &lt;/form&gt;


&lt;?php else: ?&gt;
&lt;h2&gt;Your message was sent&lt;/h2&gt;
&lt;?php endif; ?&gt;
</code></pre>
