Check if checkbox is checked or unchecked

0.24K views
no comments
18 Sep 2014 12:17 am

I was also faced that problem  the unchecked checkboxes cannot submit the data when submit form,  so I decided to a write a jquery,

here is my html

<input type="checkbox" value="1" checked="checked" named="checked_checkbox" />
<input type="checkbox" value="1" named="un_checked_checkbox" />

When I using print_r($_POST) ;
its just return

Array (
   ['checked_checkbox'] = 1
)

and here is #jQuery code to show both unchecked and checked checkboxes

$("form").submit(function () {
	//WHEN FORM WILL SUBMIT
	var this_master = $(this);

	this_master.find('input[type="checkbox"]').each( function () {
		var checkbox_this = $(this);
		//CREATE VARIABLE OF CHECKBOX_THIS

		if( checkbox_this.is(":checked") == false ) {
			checkbox_this.prop('checked',true);
			//ITS JUST CHECK THE CHECKBOX TO SUBMIT FORM DATA
			checkbox_this.attr('value','0');
		}
	})
})

now I have check the result of form data with print_r($_POST); the above #jQuery function will return checked value =1 and un-checked value return 0

Array (
   ['checked_checkbox'] = 1
   ['un_checked_checkbox'] = 0
)

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>