Home > Software engineering >  Getting checkbox value from database in CodeIgniter and ajax request
Getting checkbox value from database in CodeIgniter and ajax request

Time:01-08

I'm trying to get the value of book_active column (ENUM data type) from database. If equals 'Y' checkbox should be checked and if not, checkbox value should be 'N'.

jquery bit:

`  function edit_book(id) {
    /*    var formData = $('#form').serialize();
        console.log('Posting the following: ', formData); */

    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_id"]').val(data.book_id);
        $('[name="book_isbn"]').val(data.book_isbn);
        $('[name="book_title"]').val(data.book_title);
        $('[name="book_author"]').val(data.book_author);
        $('[name="book_category"]').val(data.book_category);
        $('[name="book_date"]').val(data.book_date);
        $('#modal_form').modal('show'); // show bootstrap modal when loaded complete
        $('.modal-title').text('Edit Book'); // Set title to Bootstrap modal title
        $('#errors').addClass('d-none');
        var date_string = dayjs(data.book_date, "YYYY-MM-DD HH:mm:ss").format("DD.MM.YYYY, HH:mm"); //Format date in form
        $('[name="book_date"]').val(date_string);

        //  $('[name="book_active"]').val(data.book_active);
        if ($('[name="book_active"]').val(data.book_active) == 'Y') {
          $('[name="book_active"]').prop("checked", true).val();
        } else {
          $('[name="book_active"]').prop("checked", false).val();

        }

        /*
              var SlectedList = new Array();
              $("input.form-check:checked").each(function() {
                SlectedList.push($(this).val(data.book_active));
              });


            
                     if ($('[name="book_active"]').val() == 'Y') {
                       $('[name="book_active"]').prop("checked", true);
                     } else {
                       $('[name="book_active"]').prop("checked", false);
                     }


                    
                     if (val(data.book_active) != NULL) {
                       $('[name="book_active"]').prop("checked", true);

                     } else {
                       $('[name="book_active"]').prop("checked", false);
                     }
                     */

      },
      // complete: function() {
      //   alert('ajax completed!');
      // },
      error: function(jqXHR, textStatus, errorThrown) {
        alert('Error get data from ajax');
      }
    })
  }`

input field in html:

`<input name="book_active" id="book_active" type="checkbox" role="switch"  <?= (isset($book->book_active)) ? set_checkbox('book_active', 'Y', false) : set_checkbox('book_active', 'N', false) ?>>`

I've tried many snippets but I'm stuck. Commented bits don't work.

CodePudding user response:

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.

  • Related