-1

I have my table with the list of checkboxes. If checkbox is checked value is set to 0, if it's unchecked then value is -1. My current code only submit check boxes if they are checked. I want to submit all check boxes in my form every time I hit save button. Is there any way I can do that? Here is my code:

HTML:

<input type="checkbox" name="user" value="~(user_ID)" class="chkBlock" />

JQuery:

//This code switch value based on if check box is checked or not
$j('.chkBlock').click(function(){
     $j(this).val(this.checked ? 0:-1);
});

and this code set check box status based on the values from DB:

$j( document ).ready(function() {
    $j('input.chkBlock[value="0"]').prop("checked", true);
    $j('input.chkBlock[value="-1"]').prop("checked", false);
    $j('input.chkBlock[value=""]').prop("checked", false);
});

My current code only submit value if it's checked.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193
  • 1
    That is how a checkbox works. If only gives a value if it's checked. That also happens if you don't use JavaScript. So actually you don't even need to check for 0 or -1. Just the presence of the value will tell you if it is checked. Embrace this behavior instead of trying to work around it. – GolezTrol Apr 04 '16 at 19:56
  • So how I can avoid work around if I have to set different value when my check-box is unchecked? And how I will save that value in DB if I can not submit unchecked check-box? Any other method that you would recommend? – espresso_coffee Apr 04 '16 at 20:01
  • 2
    Possible duplicate of [Post the checkboxes that are unchecked](http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked) – showdev Apr 04 '16 at 20:04
  • On the server just check if the value exists. For instance if it's PHP, do this: `$userChecked = isset($_GET['user']);`. After this line, `$userChecked` is a Boolean which indicates whether the checkbox `user` was checked or not. – GolezTrol Apr 04 '16 at 20:04
  • I agree with showdev that it's a duplicate, but I don't like the most commonly suggested solution of adding a hidden field and a bunch of JavaScript. It's a lot of work and a potential risk doing so, and it's not necessary. – GolezTrol Apr 04 '16 at 20:06
  • Maybe I did not understand your point but I still can not figure it out how I will submit unchecked check-box? Thanks for your time. – espresso_coffee Apr 04 '16 at 20:10
  • You don't submit unchecked checkboxes. Because you don't have to. A checkbox has two states, either checked or unchecked. If you post a form, the whole form is posted. If the checkbox's value is not in that post data, it implies that the checkbox wasn't checked. This may seem weird, but people have been building websites like this since 1993, so don't try to reinvent the wheel. – GolezTrol Apr 04 '16 at 20:11
  • What about value for that unchecked check-box? If I previously checked that check-box and I saved value 0, now I came back second time and I unchecked my value will be -1 and check-box is unchecked. That's my problem in this case. – espresso_coffee Apr 04 '16 at 20:14

2 Answers2

1
var boxes=new Array();
$(".chkBlock").each(
function()
{
  if( $(this).prop("checked") )
  {
      boxes.push( $(this).prop("name")+"[]="+$(this).val() );
  }
  else
  {
      boxes.push( $(this).prop("name")+"[]=0" );
  }
});
console.log( boxes.join("&") );

I've written the above without testing - I'm off to bed but thought I would give you some food for thought.

I believe the code I have written above will loop thru all tags with class chkBlock, if property checked is true, then it will append the name of the box, double square brackets, an equal sign, and the value of the box.

If checked is not true, then the check is still passed but given a value of zero.

EDITED: The [] means checkboxes get sent to the server as an array. Check the console log for sample data.

I hope that helps you somewhat... Or at least, I hope it does not confuse.

0

While this is not an exact answer to your post. This question has come up multiple times in different forms. So far the approach in the link below seems to be a decent solution.

Post the checkboxes that are unchecked

Community
  • 1
  • 1
peso
  • 33
  • 1
  • 6
  • 1
    If the question is answered somewhere else, mark it as duplicate. If you can't live with it until you have enough rep. Posting links to other questions as answers isn't the right way. – GolezTrol Apr 04 '16 at 20:07
  • Sorry, I'm new here I don't have enough rep to mark as duplicate. Just wanted to help the guy out. I could have copied the answer from the other post but I didn't feel that was right either. Could you mark it as duplicate? – peso Apr 04 '16 at 20:22