jdutton Posted September 4, 2013 Share Posted September 4, 2013 I'm not sure how to write a little snippet of php code. The form is at http://medicallakeveterinaryclinic.com/schedule.html. At the bottom, it asks them to put either their phone number of email address. I have a php script that checks to make sure they completed all the fields (see below). I don't know how to write an "or" line of code ($email_text OR $phone_text). Help! ___________ $first_name = $_REQUEST['first_name'] ;$last_name = $_REQUEST['last_name'] ;$pet = $_REQUEST['pet'] ;$species = $_REQUEST['species'] ;$dog = $_REQUEST['dog'] ;$cat = $_REQUEST['cat'] ;$other = $_REQUEST['other'] ;$other_species = $_REQUEST['other_species'] ;$date = $_REQUEST['date'] ;$time = $_REQUEST['time'] ;$reason = $_REQUEST['reason'] ;$contact = $_REQUEST['contact'] ;$email = $_REQUEST['email'] ;$email_text = $_REQUEST['email_text'};$phone = $_REQUEST['phone'] ;$phone_text = $_REQUEST['phone_text'};$all="first_name: ".$first_name."\r\n"."last_name: ".$last_name."\r\n"."pet: ".$pet."\r\n"."species: ".$species."\r\n"."other_species: ".$other_species."\r\n"."date: ".$date."\r\n"."time: ".$time."\r\n"."reason: ".$reason."\r\n"."contact: ".$contact."\r\n"."email: ".$email."\r\n"."email_text: ".$email_text."\r\n"."phone: ".$phone."\r\n";"phone_text: ".$phone_text."\r\n";}// If the form fields are empty, redirect to the error page.elseif (empty($first_name) || empty($last_name) ||empty($species) ||empty($date)|| empty($time)|| empty($reason) || empty($contact)) {header( "Location: $error_page" );} Quote Link to comment Share on other sites More sharing options...
requinix Posted September 4, 2013 Share Posted September 4, 2013 In this case you actually want "and": if the email address is empty and the phone number is empty (then send them to the error page). && ... || (empty($email) && empty($phone)) || ... Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2013 Share Posted September 4, 2013 In case it is not apparent from requinix's post, be sure the $email and $phone validation is within a subset of parens. When there are a bunch of conditions in the same check, I think it's a good idea to "format" the conditions so the structure is visually apparent: elseif ( empty($first_name) || empty($last_name) || empty($species) || empty($date) || empty($time) || empty($reason) || empty($contact) || ( empty($email) && empty($phone) ) ) { header( "Location: $error_page" ); } Also, I probably wouldn't do it in this situation, but if you had several fields and only require that one of them have a value you could do a single empty() check on the concatenated value if(empty($field1.$field2.$field3)) And, last side comment, you are not trim()ing the input values. So, if a user entered a space or spaces into a field it would pass your validation. I would suggest trimming the $_REQUEST value when you set the variables. E.g. $first_name = trim($_REQUEST['first_name']); Quote Link to comment Share on other sites More sharing options...
jdutton Posted September 9, 2013 Author Share Posted September 9, 2013 You guys are so wonderful. You have been a tremendous help. I will do all you suggest. You are making me look good!! Quote Link to comment Share on other sites More sharing options...
vinny42 Posted September 9, 2013 Share Posted September 9, 2013 Also, don't use empty(), because empty() will also say that a var is empty if it contains the number zero, which is a very interesting bug to have for example when your form asks for "how many cars do you own" and complains that you are not allowed to have zero cars. Generally speaking: check variables for the content that you *do* want, not for the content you don't want, because you don't know what you don't want, that's why you don't want it. :-) For example, "hello" is not empty, but it's not a date either. 2013-02-31 is a date, but not a valid one. Lots of tutorials exist to help you validate your strings properly, it's not difficult and saves you hours of bugtracking. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.