Jump to content

Set post value of checkbox to empty


mallen

Recommended Posts

Is it possible to post an empty value for a check box? Currently the table is empty. After I have checked it and when posted a value of "1". But what I want to do it when I uncheck and re-post have it blank again and not "0" as the value. Is this possible?

Link to comment
Share on other sites

You're not being very clear.  Are we to assume that you are using the checkbox value to post something to a database table?  And that when un-checking the box you want to unset whatever field you previously set to a '1' to some null value rather than a '0'?

If my guess is correct then setting it to a blank value would depend upon how you have defined your table structure.  Have you allowed this column to have a null value?

Link to comment
Share on other sites

As Barand has pointed out you have to be sure about what you actually get in your POST since checkboxes don't get sent to you if they are not checked.  I missed that in my first reply.

But - Barand's solution will give you a 0 to be posted and that is not what I think you wanted to happen.

You will have to show us your query statement that is posting this column.  Does it have a value of null for this column/field?  Or does it have a value of 'null' which is a string value that will be incompatible with an integer column definition.

Link to comment
Share on other sites

Thanks. I guess I was assuming I could have the data blank instead of NULL.  If its blank in the database, I can check it and it becomes 1. If I save, go back and edit it, resubmit it becomes "0".

<input type="checkbox" name="feature" value="1" <?php if($editProduct && $vals['feature'] == 1) echo "checked"; ?> />

I do not have it set for NULL. I have TINYINT, default, none

Link to comment
Share on other sites

You do not have the Null setting checked? 

Think about it - you have a definition of a numeric type but you want to put a (in your words) 'blank' in it.  That is a string value.  No can do.

If you want to record either a 1 or a null value you can do it if you check the Null setting in your database definition of this field. 

Please show us your actual query statement that is setting your value so we can be sure it is being done correctly.

Link to comment
Share on other sites

Several options if the feature box is unchecked.

INSERT INTO products (`prod_name`, `prod_model`, `feature`) VALUES('$name', '$model', null);

// or

INSERT INTO products (`prod_name`, `prod_model`) VALUES('$name', '$model');

 

Note there are no quotes around null in the first option (quotes would give the literal string "null" instead of a null value. However you should be using a prepared statement and passing parameter values to the query instead of putting user-supplied values directly into the query string.

$stmt = $pdo->prepare("INSERT INTO products (`prod_name`, `prod_model`, `feature`) VALUES(?, ?, ?);

$feature = isset($_POST['feature']) ? $_POST['feature'] : null;

$stmt->execute( [ $_POST['prod_name'], $_POST['prod_model'], $feature ] );

 

Edited by Barand
Link to comment
Share on other sites

Purely speaking to your database design, in general, avoid NULL.  A null value is essentially a state of value unto itself.  In your case it sounds like you want to have a tinyint with default 0.  That way if you do an insert without specifying a value, 0 will be inserted, which is generally compatible with 'false' in PHP.

This is not a PHP issue but rather an HTML forms issue.  If you have an 'unchecked' checkbox on a form, the checkbox will not exist in the $_POST.  So in situations where you show a form and the box is unchecked, you won't find that form element at all.

You have options as to how you want to handle that:

  • Use some client-side javascript to set the checkbox value to something on the submit
  • Check serverside and if you don't find the checkbox name in the $_POST, then interpret that as "unchecked" and add logic to your INSERT/UPDATE accordingly.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.