PHP isset not working with checkbox

13,820

Solution 1

If they are in your form then they will be set. You likely want to check if they are false. Here's what it is for one, you can combine the others:

if( !isset($_POST['maandag']) || !$_POST['maandag'] ) { 
     // do something
}

Secondly the code as is means that you'll only get the message if none of the checkboxes are set. I'm not certain, but if you're trying to throw an error message don't you want it if any of them are not set? In which case you need || (or) not && (and).

Solution 2

You can try doing a print_r($_POST) and see what the values of the fields are. That might help you determine what is going on with your submission/field values.

Solution 3

i thing ur coding make complicate.,,

try like

<input type="checkbox" name="test[]"  value="1"/>
<input type="checkbox" name="test[]" value="2"/>
<input type="checkbox" name="test[]" value="3"/>

submit the value and get the result

Share:
13,820
Deep Frozen
Author by

Deep Frozen

Developing in PHP with Symfony, MariaDB, Twig, SCSS and TypeScript

Updated on June 04, 2022

Comments

  • Deep Frozen
    Deep Frozen almost 2 years

    I have a form where people need to submit information. This part of code is not working how I want it and I don't know why..

    if (!isset($_POST['maandag']) && !isset($_POST['dinsdag']) && !isset($_POST['geenvoork']))
    {
        echo "Je bent vergeten in te vullen wanneer je aanwezig bent.";
    }
    

    the posts do work in the rest of the form, and are correct. What I want is to check if those 3 checkboxes are not checked. What am I doing wrong?

  • Marc B
    Marc B over 12 years
    empty() is not reliable. if the checkbox's value on the browser side is '0', empty() will be true. Any value in PHP which can be cast to an empty string counts as empty().
  • Deep Frozen
    Deep Frozen over 12 years
    It should be able to check if all 3 checkboxes aren't checked. If they aren't checked, show the message. if 1 or more are checked, don't show it. Anyway, this worked: if (!isset($_POST['maandag']) && !$_POST['maandag'] && !isset($_POST['dinsdag']) && !$_POST['dinsdag'] && !isset($_POST['geenvoork']) && !$_POST['geenvoork'])
  • Deep Frozen
    Deep Frozen over 12 years
    @yi_H it may be ugly, but it works ;) i'll try to optimize another day.. main thing for now is that it works :D
  • Cr_U
    Cr_U about 10 years
    I think it is still relevant as the solution and fucntion itself is not outdated. Besides someone could find it useful not having to repeat the same test all over again. I almost always use google to find the solutions and then look through the first few answers...