Jump to content

Check field content depending on other field entries


keyboarder

Recommended Posts

 

 

(Another quite newbie...)

I have already an online booking form. The Form works fine now and I would like to add on one more issue, but the code ignores what I want to check. I have 4 fields: arrival, departure, no. of persons and comments to check.

 

Scenario 1:

 

All field mentioned above are emtpty: Workes fine and message appears: "You have not provided any booking details".

 

Scenario 2:

 

If arrival (date_start) and departure (date_end) is entered, there should be at least an entry either in the field "comment", or in the field "pax". If not, there should be a message: "You have not provided sufficient booking details".

 

INSTEAD: The booking request is sent, which should not be the case.

 

The code is currently:

 

# all fields empty : arrival, departure, pax and comments - error

 

if(empty($data_start) && empty($data_end) && empty($pax)&& empty($comment)){

    exit("You have not specified any booking details to submit to us.

    <br>Please use your browser to go back to the form.

    <br>If you experience problems with this Form, please e-mail us");

    exit;

}

#If arrival  and departure date is entered, there should be at least an entry

 

either in the field "comment", or in the field "pax".

 

if(!isset($data_start) && !isset($data_end) && empty($pax) && empty($comment)){

    exit("You have not provided sufficient booking details.

    <br>Please use your browser to go back to the form.

    <br>If you experience problems with this Form, please e-mail us");

    exit;

}

 

The form can be tested at http://www.phuket-beachvillas.com/contact-own/contact-it.php

 

Can someone please check and tell me what's wrong with the code ? Thanks to anyone for any hint.

Link to comment
Share on other sites

This isn't doing what you think it is:

 

if(!isset($data_start) && !isset($data_end) && empty($pax) && empty($comment)){ 

 

If $data_start is set (IE. if the user did input a start), !isset($data_start) will return FALSE.

 

Therefor, if the user inputs a start date and/or an end date, your "if" is ignored (because you have FALSE && FALSE && TRUE && TRUE  which equals FALSE which means: do not follow the "then" look for an "else").

 

What you want is:

 

if (!empty($data_start) && !empty($data_end) && empty($pax) && empty($comment)) {

 

Whenever possible, it's good to conditionalize the "accepted" input, and make the default action (if all the if statements are ignored) an error message. That way if there's a mistake in your if statements, the result will usually be an error message.

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.