0

I have two check boxes in my form:

<div class="label-input-wrapper pickup">    
        <div class="form-label">I need Airport pick-up</div>
        <div class="form-input">
            <input type="checkbox" name="pick_up" value="Yes" />Yes 
            <input type="checkbox" name="pick_up" value="No" />No
            <div class="error-msg">
                <?php if(isset($errors['pick_up_no'])) { echo '<span style="color: red">'.$errors['pick_up_no'].'</span>'; } ?>
            </div>
        </div>
</div>  

Variable that saves the value of the above check boxes: $pickup = $_POST['pick_up'];

Validation:

//Check the airport pickup and make sure that it isn't a blank/empty string.
        if ($pickup == ""){
            //Blank string, add error to $errors array.        
            $errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
        }       

        if (($pick_up = Yes) && ($pick_up = No)) {
            //if both selected, add error to $errors array.        
            $errors['pick_up_no'] = "Can not select Yes and No together!";
        }

But the above validation just printing Can not select Yes and No together! if both are NOT selected, or if only one selected or even both are selected. It gives same error message for all the selections. Why?

5
  • nop.. I just want to use check boxes only.. I like that name @Ghost Commented Sep 19, 2014 at 12:31
  • You should treat the values Yes and No as strings - so put quotes around them! Commented Sep 19, 2014 at 12:33
  • @Adimeus I've changed to if (($pick_up = "Yes") && ($pick_up = "No")) but still the result is same. Commented Sep 19, 2014 at 12:36
  • @RatanK I've updated with your code. Now it's showing proper error message if both are NOT selected. It's just not printing nothing if both selected. Commented Sep 19, 2014 at 12:38
  • @RiffazStarr Ghost's answer worked (for me). Thought you'd like to know, yet I still feel that radio buttons should be used instead. Commented Sep 19, 2014 at 14:17

4 Answers 4

3

You're presently assigning with a single = sign instead of comparing == with

if (($pick_up = Yes) && ($pick_up = No))

you need to change it to

if (($pick_up == "Yes") && ($pick_up == "No")){...}

while wrapping Yes and No inside quotes in order to be treated as a string.


Edit:

Your best bet is to use radio buttons, IMHO.

Using checkboxes while giving the user both Yes and No options shouldn't be used; it's confusing.

It should either be Yes OR No and not and.

<input type="radio" name="pick_up" value="Yes" />Yes 
<input type="radio" name="pick_up" value="No" />No

That is the most feasible solution.

Sign up to request clarification or add additional context in comments.

14 Comments

I've updated to if (($pick_up == "Yes") && ($pick_up == "No")) { //if both selected, add error to $errors array. $errors['pick_up_no'] = "Can not select Yes and No together!"; } but it's just printing nothing if bot are selected..
I've made an edit with if (($pick_up == "Yes") || ($pick_up == "No")) try an OR operator @RiffazStarr or see Ghost's answer.
Plus, as Ghost states in his answer, you need to assign both checkboxes as name="pick_up[]" to be treated as an array. @RiffazStarr Did you try Ghost's answer in conjunction with mine?
@Ghost I've no more time to spend on this right now. OP would be better off using radio buttons, IMHO. Am not sure if OP tried yours. I'll have to come back later to see if I can make it work.
Thanks for your all effort @Fred-ii- I changed check box to radio button as suggested. now it works fine. So I up voted your answer and accept his answer.
|
2

I don't know why you need a checkbox for this (this is what radio button's are supposed to do), but you can do it something like:

if(!isset($_POST['pick_up']) || count($_POST['pick_up']) > 1) {
    echo 'please select at least 1 choice';
} else {
    echo $_POST['pick_up'][0];
}

Then make your checkbox names:

name="pick_up[]"

Or if you really need to have separate error messages. Do something like this:

$errors['pick_up_no'] = '';
if(!isset($_POST['pick_up'])) {
    $errors['pick_up_no'] = "Please let us know your airport pick up requirement!";
} elseif(count($_POST['pick_up']) > 1) {
    $errors['pick_up_no'] = "Can not select Yes and No together!"; // weird UX, should have prevented this with a radio button
}

if($errors['pick_up_no'] != '') {
    echo $errors['pick_up_no'];
} else {
    echo $_POST['pick_up'][0];
}

4 Comments

I tend to think that this would work, although I haven't tested it yet. OP should give some input if it did or not. I'm right in the middle of something, so I'll come back later to let you know if it does, I will definitely +1 ;) yet I tend to think that radio buttons is a more feasible option as I've noted in my edit. Checkboxes for both yes AND no doesn't make sense, IMHO.
@Fred-ii- yes actually i tested this before posting, but i also agree with your sentiment, this should be a radio button, no doubt, thats weird for a user experience to actually choose both yes and no
It's boils down to a "wishy washy" decision lol - It's yes or no. Pick me up at 8 and don't be late hahaha!
@Fred-ii- well at heart, really my original answer is the one that utilizes the radio button which is most logical thing to use. anyway, hope the user tries this.
1

It is impossible for $_POST['pick_up'] to be both yes and no at the same time since the name is pick_up and not pick_up[] <- pointing to multiple variables. So it will either be yes or no either way because one parameter will overwrite the other. You would have to use javascript to verify only one is checked before submit in that case.

2 Comments

Follow this and convert name of checkboxes pick_up[] and change like this if (($pick_up == 'Yes') && ($pick_up == 'No'))
Why the ** did i get downvoted on this. My answer is absolutely correct. As his code currently is, it is impossible for the _POST parameter to contain both yes and no at the same time.
1

Use array of checkbox field elements;

<input type="checkbox" name="pick_up[]" />Yes 
<input type="checkbox" name="pick_up[]" />No

then in your PHP script:

<?php

$pick_ups=$_POST['pick_up'];

$first=isset($pick_ups[0]); //true/false of first checkbox status
$second=isset($pick_ups[1]); //true/false of second checkbox status

?>

Again, as you've already been told, you should use radio-buttons for this purpose! Take into account that $_POST does not send checkbox if it is not set (check with isset($_POST['name'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.