-1

Here, I have treeview with checkboxes. My problem is how can I do the condition for checked checkboxes equal to 'y' and if unchecked checkboxes equal to 'n'. I have to update in database the value of chekbox which is column active that consist two value which is 'y' and 'n'. And I'm using AJAX in javaScript. It should be checked checkbox will get 'y' and unchecked checkbox get 'n' in database. But now, I'm currently just save checked checkbox equal to 'y' in database. Anyone have suggestion and what would be the best approach? For your information, I'm using treeview Kendo UI.

update.php

function deleteTemplate(){
global $ehorsObj;
$employeeID = $_SESSION['employeeID'];
$propertyID = $_SESSION['propertyID'];
$id         = (isset($_POST['id']) ? $_POST['id'] : '');
$progid     = (isset($_POST['progid']) ? $_POST['progid'] : '');

$sqlDelete = "DELETE FROM tblHrsPositionProgramTemplate 
                WHERE hrsPositionID = '".$id."'"; 
$ehorsObj->ExecuteData($sqlDelete, $ehorsObj->DEFAULT_PDO_CONNECTIONS);

for($x=0; $x< sizeof($progid); $x++ )
{
    $positionTemplateID = $ehorsObj->EHORS_PK("tblHrsPositionProgramTemplate"); 
        $sqlAdd = "INSERT  INTO tblHrsPositionProgramTemplate 
                    SET positionTemplateID = '" . $positionTemplateID . "',
                    programID = '" . $progid[$x] . "',
                    hrsPositionID  = '" . $id . "',
                    propertyID   = '" . $propertyID . "',
                    employeeID  = '" . $employeeID . "',
                    dateTimeEmployee = NOW() ";     

        $ehorsObj->ExecuteData($sqlAdd, $ehorsObj->DEFAULT_PDO_CONNECTIONS);

        $positionTemplateIDLog = $ehorsObj->EHORS_PK("tblHrsPositionProgramTemplateLog");   
        $sqlAddLog = "INSERT  INTO tblHrsPositionProgramTemplateLog 
                    SET positionTemplateIDLog = '" . $positionTemplateIDLog . "',
                    positionTemplateID = '" . $positionTemplateID . "',
                    programID = '" . $progid[$x] . "',
                    hrsPositionID  = '" . $id . "',
                    propertyID   = '" . $propertyID . "',
                    employeeID  = '" . $employeeID . "',
                    dateTimeEmployee = NOW() ";     

        $ehorsObj->ExecuteData($sqlAddLog, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
}}

AJAX JavaScript

//AJAX call for button
    $("#primaryTextButton").kendoButton();
    var button = $("#primaryTextButton").data("kendoButton");
    button.bind("click", function(e) {

    var test = $("#dropdown").data("kendoDropDownList").value();
    if(test == ""){
            KendoAlert("Please select position");//dropdown
    }  
    else{

        $.ajax({
            url: "../DesignationProgramTemplate/getTempJson.php",
            type: "POST",
            data: {
                method: "addTemplate" ,
                id: test,
                progid: array
                },
            success: function () {                
            KendoAlert('Success'); }});
    }
    });

Output

Hikari
  • 53
  • 6

1 Answers1

0

You can easily obtain if a checkbox is checked or not (see this for example), and in your ajax, send the name of your checkbox and also the value (true/false).

<input type='checkbox' name='checkbox1' class='my-checkboxes'/>Checkbox 1

<script>
    $(document).ready(
        function () {
            $(".my-checkboxes").on('change', function () {
                let checkboxIsChecked = ($(this).is(':checked'));
                let checkboxName = $(this).attr('name');
                //do your ajax post based on the checkbox name and value
                console.log(checkboxIsChecked, checkboxName);
            });
        }
    );
</script>
Ali Khalili
  • 1,504
  • 3
  • 18
  • 19
  • I'm sorry, but I'm prefer to do the condition in update.php. Don't you think so? – Hikari Sep 18 '19 at 04:12
  • In any case, you should send the values from ajax and use those values to insert/update your db in your PHP code and if the checkbox is checked or not, you have some db operations. Right? Reading your question, I thought the problem is in you js side. Did I misunderstand? – Ali Khalili Sep 18 '19 at 04:40
  • Okay I got it. Here, I'm using kendo treeview checkbox so I'm get a little bit confused because the example you show above in checkbox html. – Hikari Sep 18 '19 at 05:04