lucasmland Posted February 16, 2012 Share Posted February 16, 2012 I created a php contact form for our Technology department in a school district. I would like to make a particular field required or optional based on the choice made in a dropdown menu.So, if the user chooses the Hardware or Software category, I want the barcode field to be required. If they choose Username/Password, I want the barcode field to be optional.Here is the part of the code that is validating fields: function died($error) { // your error code can go here echo "<b>We are very sorry, but there were error(s) found with the form you submitted.</b><br /> <br />"; echo $error."<br />"; echo "Please go back and fix these errors.<br />"; die(); } // validation expected data exists if(!isset($_POST['name']) || !isset($_POST['location']) || !isset($_POST['email']) || !isset($_POST['machine']) || !isset($_POST['problem'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $name = $_POST['name']; // required $location = $_POST['location']; // required $email_from = $_POST['email']; // required $machine = $_POST['machine']; // required $barcode = $_POST['barcode']; // not required $problem = $_POST['problem']; // required $issue = $_POST['issue']; $error_message = ""; if(($issue) == '') { $error_message .= 'Please select a category.<br />';} if(($issue) == "Hardware" || "Software" && ($barcode) == '') { $error_message .= 'The Service Tag/Barcode you entered does not appear to be valid.<br />'; } $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 you entered does not appear to be valid.<br />'; } $string_exp = "/^[A-Za-z0-9, .'-]+$/"; if(!preg_match($string_exp,$name)) { $error_message .= 'The Name you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$location)) { $error_message .= 'The Location you entered does not appear to be valid.<br />'; } if(!preg_match($string_exp,$machine)) { $error_message .= 'The Machine you entered does not appear to be valid.<br />'; } if(strlen($problem) < 2) { $error_message .= 'The problem you entered does not appear to be valid.<br />'; } if(strlen($error_message) >= 1) { died($error_message); } The problem is that this always setting $error_message to 'The Service Tag/Barcode you entered does not appear to be valid.<br />'. So, when user selects Username/Password the error message is still preventing the script from sending the email.Any ideas?Lucas Land Link to comment Share on other sites More sharing options...
justsomeguy Posted February 16, 2012 Share Posted February 16, 2012 if(($issue == "Hardware" || $issue == "Software") && $barcode == '') Link to comment Share on other sites More sharing options...
lucasmland Posted February 17, 2012 Author Share Posted February 17, 2012 I knew it was something in the syntax I was getting wrong. Thanks it works like a charm! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.