PHP contact form - making a field not mandatory -
how can make "email" field not mandatory? if not filling field, form should submit.
in below code, "email" field mandatory.
i tried add if !isset
email field $email_from
word "empty", didn't work me.
<?php if(isset($_post['name'])) { // edit 2 lines below required $email_to = "aaaaa@aaaaa.com"; $email_subject = "messeage site"; function died($error) { ?> <?php die(); } // validation expected data exists if(!isset($_post['name']) || //!isset($_post['email']) || /* tried comment line, didnt work. */ !isset($_post['telephone'])) { died('we sorry, there appears problem form submitted.'); } $name = $_post['name']; // required $email_from = $_post['email']; // required $telephone = $_post['telephone']; // not required $error_message = ""; $email_exp = '/^[a-za-z0-9._%-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'the email address entered not appear valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "first name: ".clean_string($name)."\n"; $email_message .= "email: ".clean_string($email_from)."\n"; $email_message .= "telephone: ".clean_string($telephone)."\n"; // create email headers $headers = 'מאת: '.$email_from."\r\n". 'חזור ל: '.$email_from."\r\n" . 'x-mailer: php/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <?php echo '<meta http-equiv="refresh" content="0; url=thank_you.html">'; exit; ?> <?php } ?>
if(!isset($_post['name']) || !isset($_post['telephone'])){ if(isset($_post['email'])){ //all code email inside here } }
this should trick, while $_post['email'] empty should´t bother anymore.
Comments
Post a Comment