raqulka raqulka - 11 months ago 41
MySQL Question

Change checkbox value and insert to database

Sounds simple enough, but I just cannot make it work. I am trying to insert a checkbox value to database. If it checked, insert Y, if not, insert N. In my DOM, I see that the value of checkbox is changing to N after I click it, but the values are not changed in database. Maybe it is something small and stupid that I am missing.

So the default value is N.

Here's the HTML:

<div class="funkyradio">
<div class="funkyradio-success">
<input type="checkbox" name="animalCare" id="animalCare" value="N"/>
<label for="animalCare">Hoolitsen loomade eest:</label>


//I have a lot more of those checkboxes
$('#cleaningOk, #animalCare, #homeSchooling, #worktime_lenght, #sickChildren, #isSmoking, #isTattoo, #animalsOk, #disabledChildren, #singleParent, #driversLicense, #babyOk').change(function(){
if ($(this).is(':checked')) {
} else {

For inserting I use formValidation.io plugin and if the checkbox is changed, I don't even see the N in formdata under the network tab, else I see the Y.

.on('success.form.fv', function(e, data) {
// Prevent form submission

var $form = $(e.target),
formData = new FormData(),
params = $form.serializeArray(),
files = $form.find('[name="userProfilePhoto"]')[0].files;
url: $form.attr('action'),
data: formData,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data) {},
error: function(jqXHR, textStatus, errorThrown, data) {
console.log(jqXHR, textStatus, errorThrown, data);


if (isset($_POST["cleaningOk"], $_POST['animalCare'], $_POST['homeSchooling'])){
$cleaningOk = $_POST['cleaningOk'];
$animalCare = $_POST['animalCare'];
$homeSchooling = $_POST['homeSchooling'];

$nanny_services = $user_home->runQuery("INSERT into services (user_id, koristab, loomade_hoolitsus, koduõpetamine) VALUES (:user_id, :cleaningOk, :animalCare, :homeSchooling) ON DUPLICATE KEY UPDATE koristab= VALUES(koristab), loomade_hoolitsus= VALUES(loomade_hoolitsus), koduõpetamine=VALUES(koduõpetamine)");
$nanny_services->bindparam(':cleaningOk', $cleaningOk, PDO::PARAM_STR);
$nanny_services->bindparam(':animalCare', $animalCare, PDO::PARAM_STR);
$nanny_services->bindparam(':homeSchooling', $homeSchooling, PDO::PARAM_STR);
$nanny_services->bindparam(':user_id', $user_id, PDO::PARAM_STR);

$response_array["status"] = 'success';


Don't try to change the value on the client. Checkboxes are not designed to work that way. If the checkbox isn't checked, it won't appear in the submitted data.

Test if the data is sent instead.

$value = "N";
if (isset($_POST['animalCare'])) {

(Obviously, don't make the value being sent from the client a requirement of the query to be run at all).