How to insert Checkbox value to database

I need one checkbox data to send to mysql database using php

my database is “mydatabase”
table name “datatb”
column name “show”

when ckeckbox is ckecked it should insert “yes” into column “show”
when ckeckbox is unckecked it should insert “no” into column “show”

:shifty: :shifty:

1 Like

You are asking for the php code do do this? Your post says you "need one checkbox data "?

yes, I want to use only one checkbox.

…and what is the problem you are having with it?

Here is the general idea:

$query = "INSERT INTO datatb(show) VALUES('";

if (isset($_POST['checkbox_name']) && ($_POST['checkbox_name'] == "value")) {
 $query .= "yes";
} else {
 $query .= "no";

$query .= "');";


Obviously, you include whatever other fields you have in the table, and perform appropriate checking / escaping where relevant. It also assumes you have opened a DB connection already.

according to your script checkbox should be like this?

<input type=“checkbox” name=“Ckekboxname” value=“yes” >

The values for the checkbox are context sensitive, depending on how you intend to use them.

Tip: If the checkbox is not checked, it’s not even included in the submitted form.

The check for ($_POST[‘checkbox_name’] == “value”) means that you should replace the name and value with whatever makes sense from your page.

<input type="checkbox" name="receive_pamphlets" value="yes">

if (isset($_POST['receive_pamphlets']) && ($_POST['receive_pamphlets'] == "yes")) {
 $query .= "yes";
} else {
 $query .= "no";

An alternate method that uses HTML rather than an if statement in PHP to test if the field exists.

<input type="hidden" name="receive_pamphlets" value="no">
<input type="checkbox" name="receive_pamphlets" value="yes">

With the HTML coded that way ‘yes’ will be passed if the checkbox is checked and ‘no’ will be passed if it is not.

using the over-ridden hidden form field is a very nice way to ensure that some value gets sent.

Should he perform the test in PHP anyway, as a part of validation to make sure someone hasn’t tried to get a bad value in there?

I usually use integers for yes no values and translate them if needed on output for display. In that case, you could still use the hidden field, but if the value wasn’t present it would still register as 0 or no when it was type cast ($choice = (int) $_POST[‘checkboxname’]). The hidden field method is especially nice if you have a lot of checkboxes and need to keep track of the numerical order regardless of which one is checked and which one isn’t.

You should always validate everything from the form on the server to ensure that the values passed are valid and have not been tampered with.

Actually, value is a required attribute for checkboxes.

But you cannot have two elements with the same name in valid HTML, surely?

You’re right, I should’ve checked the spec before coming up with that. It’s a good thing I’ve used a value on them as a matter of cause.

Yes you can, just think about how radio buttons work.

Checking the spec shows the following

Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property.

Ah, ok. So how does it work when you have multiple values selected? How to they get returned in $_POST? As an array?

In the example of the hidden field, the value that is transmitted is the last non blank value in source code order. Therefore if the checkbox is not checked what comes through is the value of the hidden field. If the checkbox is checked, then it is last in source code order and will have it’s value sent.

You can however use array syntax with any kind of form field.

<input type="checkbox" name="chkname[]" value="1" />

<select name="date[0]">
   <option value="1">January</option>

<input type="text" name="modelnumber[]" />

Any of those will work, and bring back an array to PHP. In fact if you want to use a multiple select list, you have to use array syntax for the name, in order for PHP to receive all the selected items.

Keep in mind, that Javascript doesn’t look at the array syntax in the name, only PHP. To javascript the name attribute is a literal string, regardless. Items named the same thing are part of a named node map, but naming a form control…

<input type="checkbox" name="favoritefood[0]" value="chicken" />

means nothing to Javascript. You would still address it like…


Cool, cheers

I knew about the array syntax, but not about having multiple names the same without the array syntax.

I always pass a numeric value in the checkbox field as Hammer65 has shown:

<input type="checkbox" name="active" value="1" />

Then since checkboxes don’t pass any value (they are not set) if they aren’t checked, it’s easy to test for on the backside with isset():

$active = isset($_POST['active']) ? 1 : 0;

The you save the NUMERIC value into your database in a TINYINT(1) field for MySQL (for other databases, you can use a proper BOOLEAN column type). Any other time you need to test the value, you can just put the variable from your database into an ‘if’ statement, and if becomes very easy since 0 evaluates as false:

if($record->active) {
  // do stuff

Numeric 1/0 values are easier to store and understand across languages, easier to test for, and require less disk space to store than text. Always use numeric 1/0 values for simple on/off stored data.

A common error some newbies make in their forms is to give several fields the same name without using an array and then wonder why only the last of those fields actually gets passed. The use of a hidden field with the same name as a checkbox is one of the few instances where giving two fields the same name is meaningful - the only other one I can think of is radio buttons. In each case only one value will be passed with that value decided by which radio button is selected or whether the checkbox is or isn’t checked. The only other time it would work for multiple fields having the same name without using an array is if you use JavaScript to control which of them are actually in the form.

I think it would be more efficient to process the checkbox on the other side, rather than have to send out an extra hidden field every time, but ah well