开发者

PHP isset not working with checkbox

I have a form where people need to submit information. This 开发者_如何学Pythonpart 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?


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.


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).


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


Set the proper value for you checkbox in HTML.

<input type="checkbox" ... value="1">

Your current accepted solution just complicates the PHP code.


I just made a check upon a checkbox I am using and here are the values:

<input id="Isolated1" type="checkbox" value="1" name="Isolated1"></input>

In case when input is checked the following evaluates as:

isset( $_POST["Isolated1"] )

//True

( $_POST["Isolated1"] )

//True

In case when input is NOT checked:

isset( $_POST["Isolated1"] )

//False

( $_POST["Isolated1"] ) 

//Notice: Undefined index

Therefore, I suggest using empty() function, simultaneously checking whether variable does not exist or its value equaling FALSE.


As prisonor said you need to check if it is empty(), although if it's not set at all this could give you a notice.

Your if would require:

isset($_POST['checkbox']) && !empty($_POST['checkbox'])

Edit: Stupid me, your right it doesnt return anything. Checking your post with print_r as posted below sounds like the best option.


Actually that should work...

You could always set your checkbox to a value and specifically check that that value is returned:

<input type="checkbox" name="maandag"  value="1"/>

<?php if(isset($_POST['maandag']) && $_POST['maandag'] != '1') { /* ... */ }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜