Alex
Alex

Reputation: 1073

Multiple Checkboxes array with checked and unchecked values

So i have this line of code that will repeat different times in a form.

<input type="checkbox" name="checkbox[]" /> !checked
<input type="checkbox" name="checkbox[]" /> !unchecked
<input type="checkbox" name="checkbox[]" /> !checked
<input type="checkbox" name="checkbox[]" /> !unchecked

The !checked show that the checkbox was checked and the !unchecked shows that the checkbox was not checked.

How can i create a php array to get values of checked and unchecked checkboxes in order like this :

array( 0 => checked, 1 => unchecked, 2 => checked, 3 => unchecked );

Momentarily i can get just the checked value with $_POST["checkbox"] but i cannot get the unchecked value.

Upvotes: 3

Views: 9821

Answers (6)

sarbudeen
sarbudeen

Reputation: 170

Please check below code.

     $("button").click(function () {
     var i=0;
     var cbox=[]; 
        $("input[name='cbox']").each(function () {
            if($(this).is(':checked')) {
                cbox[i++] = $(this).val();
           }else{
                cbox[i++] = "unchecked";
           }
         });
         console.log(cbox);
          i = 0;
               
     });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="checkbox" value="checked" name="cbox">
<input type="checkbox" value="checked" name="cbox">
<input type="checkbox" value="checked" name="cbox">
<input type="checkbox" value="checked" name="cbox">
<button>Click</button>

Upvotes: 0

brianlmerritt
brianlmerritt

Reputation: 2852

If you are using a server side language like PHP, there is an easier method than using hidden fields to supply default or writing javascript (both may fail if the user's device/browser doesn't support that method).

    <input type="checkbox" value="choice1" name="checkbox[]" />
    <input type="checkbox" value="choice2" name="checkbox[]" />
    <input type="checkbox" value="choice3" name="checkbox[]" />

This method doesn't return unchecked items, but it specifically identifies which items were checked. Otherwise, if the checkboxes all have the same value, all you get is one, two or 3 values repeated with no idea which item was checked. However, assuming choice2 was checked with the above method, it's pretty easy then to figure out that item 1 and 3 therefore were not checked.

Upvotes: 0

Solved:

Declaration of form...

<form id="form1" name="form1" method="post" action="xx.php" onSubmit="set_hidden_value()">
<input name="arrayofchecks" type="hidden" value="toset" />
...

OnSubmit:

function set_hidden_value()
{
var checkstring = "";
for (var i=0; i < $('#checkbox').length; i++)
{
    if ($('#checkbox')[i].checked)
    {
     checkstring = checkstring + "1";
    }
    else
    {
     checkstring = checkstring + "0";
    }
}
$('#arrayofchecks').val(checkstring);

And the result is a string with values checked and unchecked (1 and 0)...

In my case, i use ajax for intercept submit and do set_hidden_value here...

Upvotes: 0

user1607528
user1607528

Reputation:

This will print only checked fields, because unchecked ones are not sent to server. You will have to do some javascript and hidden field tricks.
Take a look here

Post the checkboxes that are unchecked

<input type="checkbox" name="checkbox[n1]" /> !checked
<input type="checkbox" name="checkbox[n2]" /> !unchecked
<input type="checkbox" name="checkbox[n3]" /> !checked
<input type="checkbox" name="checkbox[n4]" /> !sdsk

 foreach($_POST['checkbox'] as $key => $value){
    $checkbox[$key] = 'checked';
 }
 print_r($checkbox); // your new array

Upvotes: 0

Zim84
Zim84

Reputation: 3497

First of all you need to put a value to your checkboxes:

<input type="checkbox" name="checkbox[]" value="checkboxNchecked" /> !checked

You can't really distinguish your checkboxes otherwise.

Then: Your checkboxes will either return a value if they are checked or will be ignored when they are unchecked. You will not get a NULL, FALSE or other value. It will simply not be transfered via POST/GET to your php script as if it wasn't in yout HTML code. This covers the topic: Does <input type="checkbox" /> only post data if it's checked?

If you know how many checkboxes are around and what they are called - no problemo señor - but if you don't, you'll need to find a way around. If you tell us what the nature of your checkboxes are, we can help you find a tailored solution.

Upvotes: 1

Awlad Liton
Awlad Liton

Reputation: 9351

you can use jquery and ajax. In your submit event get all values from the form and submit it by ajax. you can get unchecked value in jquery like this:

$("input:checkbox:not(:checked)") 

or

if ($('#idOfYourCheckBox:checked').length > 0) {

     //its checked
} 
else {
//not checked
}

Upvotes: 0

Related Questions