How to validate a checkbox input using PHP?

匿名 (未验证) 提交于 2019-12-03 01:26:01

问题:

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?

回答1:

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]; } 


回答2:

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.



回答3:

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.



回答4:

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'])



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!