0

I store details about books in table called books. One of the columns is called "book_active" and holds values "Y" or "N".

I want my checkbox named "book_active" to be checked if value in mentioned column equals "Y" and unchecked if value equals "N".

Currently after sending the form the value of that checkbox is "NULL" in database. I'm stuck how to approach this.

<input name="book_active" id="book_active" type="checkbox" role="switch" class="form-check-input" value="Y">
<script>
function edit_book(id) {
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$.ajax({ //Load data from ajax
url: "<?php echo site_url('book/ajax_edit/') ?>" + id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('[name="book_active"]').val(data.book_active);
//selecting checkbox - doesnt work
if (data.book_active == 'Y') {
$('#book_active').prop('checked', data.book_active == 'Y'); //.prop (name, value)
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error get data from ajax');
}
})
}
</script>
janeiro
  • 25
  • 7

2 Answers2

0

Your description of the goal, “If equals 'Y' checkbox should be checked and if not, checkbox value should be 'N’.” is a little confusing. If you want to set the checkbox’s state depending on the value of data.book_active, this ought to do it:

$('[name="book_active"]').prop("checked", data.book_active == 'Y’)

That will check the checkbox if the value is ‘Y’ and clear it if it is anything else.

padeso
  • 390
  • 1
  • 8
  • Now I've got this code but when I'm trying to send form I'm getting error 'field active required'. When I delete validation rules for that field the form gets send but data inserted to book_active column is NULL. `if (data.book_active == 'Y') { $('[name="book_active"]').prop('checked', data.book_active == 'Y').attr('value', 'Y'); } else if (data.book_active == 'N' || data.book_active == 'NULL') { $('[name="book_active"]').attr('value', 'N'); } else { }` – janeiro Jan 08 '23 at 10:42
  • Don’t bother trying to set the value of the checkbox. When a form is posted, if a checkbox is set, the payload will include `=`, but if it is clear, its name won’t be included in the data at all. The MDN web docs have a good explanation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/checkbox. You can either include a `` field as they suggest, or assume the value is false if it’s missing on the server side. – padeso Jan 08 '23 at 13:22
  • I'm not sure if I'm right but In my case the value of that field has to be "N" not default value "on". Currently code above can get the value from DB and check the checkbox. The problem is when I send data the checkbox value "N" is not being send but "NULL" appears in DB. – janeiro Jan 08 '23 at 21:55
  • It doesn’t matter whether the value is “Y” or “N”, because the user can check or uncheck the control, and when it’s posted, whichever value it has will only be sent if it is checked. If you can’t interpret the data that’s posted in the server, here’s a link to a page that describes an approach you might try instead: https://stackoverflow.com/a/1992745/20771004. Since it requires two elements with the same name, you’ll probably need to assign an ID to your checkbox, and use that to set its state. You’ll also need to set the values to “Y” and “N” instead of “1” and “0” like in the example. – padeso Jan 08 '23 at 23:39
0

I have resolved my problem:

deleted:

 $('[name="book_active"]').val(data.book_active);

replaced:

<input name="book_active" type="hidden" value="N">
<input name="book_active" id="book_active" type="checkbox" role="switch" class="form-check-input" value="Y">

replaced jquery bit:

if (data.book_active == 'Y') { //check the checkbox
$('#book_active').prop('checked', true);
} else {
$('#book_active').prop('checked', false);
}
janeiro
  • 25
  • 7