Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9044945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:07:05+00:00 2026-06-16T11:07:05+00:00

I have a set of checkboxes which when checked they pass the value as

  • 0

I have a set of checkboxes which when checked they pass the value as 1, otherwise they pass the value as 0. However, instead of sending the value of the unchecked checkboxes as ‘0’, the value being sent is ‘NULL’.

I have the following JS code in place that should set the value to 0/1 accordingly, however still the value is sent as NULL. Is there anything that can be done to make sure that the value passed in case of an unchecked checkbox is 0?

$('#cb1, #cb2, #cb3, #cb4').on('click', function ()
    $(this).val(this.checked ? 1 : 0);
});

UPDATED
Here is my html:

<input type="checkbox" name="cb1" id="cb1" value="1" checked />
<input type="checkbox" name="cb2" id="cb2" value="1" checked />
<input type="checkbox" name="cb3" id="cb3" value="1" checked />
<input type="checkbox" name="cb4" id="cb4" value="1" checked />

They are all checked by default.

If one of them is unchecked, MySQL is reporting the following error:
0SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘ColumnName’ cannot be null.

This is because the columns are set to set not to be Null. And even though I have set the default value to 0, once I click on the submit button I still get the error. I tried to remove the Not NULL property however rather than prefilling the value as 0, the input was NULL in the database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:07:10+00:00Added an answer on June 16, 2026 at 11:07 am

    If a checkbox is unchecked, it doesn’t get sent, so setting it’s value to 0 if it isn’t checked isn’t going to help – it will always return NULL.

    There are two ways to fix this easily:

    1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn’t always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won’t work.

    2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

    <input type="hidden" name="checkbox_1" value="0">
    <input type="checkbox" name="checkbox_1" value="1">
    

    Note: If your names are in an array form (ie they have square brackets in them), this won’t work, as the hidden fields will increment the array count as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of checkboxes and have disabled few which I want to
I have a group of checkboxes in which I want to add their checked
I have outline view each element of which is checkbox. I want to set
I have a set of checkboxes that an Admin can tick to give users
I want to have a set of checkboxes that use AJAX to post to
I have an array of checkboxes that I edit at once to set up
I have a checkbox and a textbox client which works good when they are
Relative jQuery newbie: I want to display a set of checkboxes, of which the
I have a form with a question. If answered yes, a set of checkboxes
Currently, I have a piece of code which displays four checkboxes and allows a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.