Question

very common to us to get the value of the post input type with data. but now i want to get if the checbox is check or unchecked. here is my code:

while (!$listof_artist->EOF) {
    $id = $listof_artist->fields['id'];
    $arr_id[] = $id;

    echo "<tr><td><input type='checkbox' value='".$id."' name='chkid[]' id='chkid[]' /></td>";
}

if(isset($_POST['submit'])){
    foreach($_POST['chkid'] as $checkbox){
        //code for add
    }

here is my algorithm: if submit is post then

for each checbox is check
    //add

foreach checbox not check
        //code for not check

Please help. thank you

Was it helpful?

Solution

Similar to what the other answers have already pointed out, here is a way to do it:

while (!$listof_artist->EOF) {
    $id = $listof_artist->fields['id'];
    $arr_id[] = $id;

    echo "<tr><td><input type='checkbox' value='$id' name='$id' id='$id' /></td>";
}

if(isset($_POST['submit'])){
    foreach($arr_id as $checkbox){
        if (isset($_POST[$checkbox])) {
            // Checkbox is checked
            echo 'Artist id ' . $checkbox . ' is checked <br/>';
        }
        else {
            // Checkbox is NOT checked
            echo 'Artist id ' . $checkbox . ' is NOT checked <br/>';
        }
}

OTHER TIPS

Getting a value which was not posted to your url is just like asking for names of all planets that are not yet discovered.

There is a simple workaround to what you are doing, Let's say you have 10 checkboxes. You know the names and you know the values because you created them. Go to your page where you process the form submission and tell all the names to your code. Then the code checks which one has a value in $_POST and which one does not. That gives you your answer.

Using simple HTML will not send the values of unchecked checkboxes to your form processing url.

Whenever an form is submitted only those check-boxes which are checked can be obtained via $_POST[var] so for checked its simple as in your question itself:

foreach($_POST['chkid'] as $checkbox){
  //code for add
}

but for not checked ones you will have to run a loop to match from database if its checked or not.

Or here is a better way out.

instead of

<input type='checkbox' value='".$id."' name='chkid[]' id='chkid[]' />

use

<input type='checkbox' value='$id' name='$id' id='$id' />

now you can easily check them by their name as

if(isset($_POST[$id])){
//this is checked
}
else{
//its not checked
}

NOTE: if $id contains space or is too big better asign some counting variable as name instead.

Here is an small test code run it and it will make you understand better.

<?php
if(!empty($_POST['c']))
    print_r($_POST['c']);
?>
<form action="" method="post">
    <input type="checkbox" name="c[]" value="1" />
    <input type="checkbox" name="c[]" value="2" />
    <input type="checkbox" name="c[]" value="3" />
    <input type="checkbox" name="c[]" value="4" />
    <input type="checkbox" name="c[]" value="5" />
    <input type="submit">
</form>

As others have stated you can't get the name/value of the form elements that haven't been sent, however before you submit your form, with a little bit of JavaScript, you can find out which checkboxes haven't checked, collect their names/values and manually attach it to your form data and send it to the server.

Just as a hint, this code collects the unchecked checkboxes in your form and then you can send it, for example, via a hidden input field or parameter in your Ajax request, to the server.

$("input:checkbox:not(:checked)")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top