Change this section:
foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != '') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } }
to ...
foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != '') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; } } } $email_content .= $value) .': '.$_POST[$value])."\n"; }
To simplify, you take each element in your $values array and look for it in the form. If it is on the $required list, then you check to see if it is empty. If it is empty, an error message is displayed. Otherwise, you want to add it to the message that will be sent by mail ( $email_content ).
Previously, the “add it to email content” part was only performed in the “if required” cycle. I moved it down so that it is part of "for each value loop.
Chris source share