mallen Posted May 29, 2018 Share Posted May 29, 2018 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? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 29, 2018 Share Posted May 29, 2018 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 29, 2018 Share Posted May 29, 2018 (edited) Unchecked checkboxes are not sent in the POST data. If the checkbox's name is "checkbox" then $checkbox_value = isset($_POST['checkbox']) ? 1 : 0; Edited May 29, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 29, 2018 Share Posted May 29, 2018 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. Quote Link to comment Share on other sites More sharing options...
mallen Posted May 30, 2018 Author Share Posted May 30, 2018 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted May 30, 2018 Share Posted May 30, 2018 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. Quote Link to comment Share on other sites More sharing options...
mallen Posted May 30, 2018 Author Share Posted May 30, 2018 INSERT INTO products (`prod_name`, `prod_model`, `feature`) VALUES('$name', '$model','$feature') Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2018 Share Posted May 30, 2018 (edited) 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 May 30, 2018 by Barand Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 30, 2018 Share Posted May 30, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.