Jump to content

Need to require either one or another option be completed in form


jdutton

Recommended Posts

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" );
}
 

Link to comment
Share on other sites

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']);
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.