In-Place Editing in DataTable with X-Editable using PHP Ajax

171 Views Asked by At

I use DataTable to display data, but now I need to change the data in the table.

enter image description here I found a tutorial on youtube that uses the X-Editable library for that purpose. I adjusted the functionality and everything was fine until I got to the data update part. In that tutorial, he uses 2 parameters that he uses when executing the query, which he places in the post (name and id), but what I need is the third parameter for the year, and I have never been able to figure out how to place the value for the year in the post and send it to the update .php. I hope I explained well. Below is the code, I hope someone has an idea how to do this. Thanks in advance

This is how I get the data and display it in a table

$(document).ready(function(){
var dataTable = $('#sample_data').DataTable({
    "processing": true,
    "serverSide": true,
    "order":[],
            "pageLength": 100,
    "ajax":{
        url:"fetch_mesec.php",
        type:"POST",
    },
    createdRow:function(row, data, rowIndex)
    {
        $.each($('td', row), function(colIndex){
                            if(colIndex == 5)
            {
                $(this).attr('data-name', 'januar');
                $(this).attr('class', 'januar');
                $(this).attr('data-type', 'number');
                $(this).attr('data-pk', data[3]);
                $(this).attr('data-year', data[4]);

            }
                            if(colIndex == 6)
            {
                $(this).attr('data-name', 'februar');
                $(this).attr('class', 'februar');
                $(this).attr('data-type', 'number');
                $(this).attr('data-pk', data[3]);
                $(this).attr('data-year', data[4]);

            }
                            if(colIndex == 7)
            {
                $(this).attr('data-name', 'mart');
                $(this).attr('class', 'mart');
                $(this).attr('data-type', 'number');
                $(this).attr('data-pk', data[3]);
                $(this).attr('data-year', data[4]);

            }
           // ...
        // I removed the repetitive code, so there is less code
        });
    }
});

This code works fine, the problem now starts when I want to do a single row upgrade

$('#sample_data').editable({
    container:'body',
    selector:'td.januar',
    url:"update.php",
    title:'Januar',
    validate:function(value){
        if($.trim(value) == '')
        {
            return 'Obavezno polje';
        }
    }
       
});

The query should look like this

include_once("private/initialize.php");
$query = "UPDATE mesec SET ".$_POST["name"]." = '".$_POST["value"]."' 
WHERE customer_no = '".$_POST["pk"]." AND godina = '".$_POST["year"]."'";
$dbc->query($query);

It is clear to me that it takes the value for name from $(this).attr('data-name', 'januar'); and places it in $_POST["name"]. The same goes for the customer code, take the value from $(this).attr('data-pk', data[3]); and put it in $_POST["pk"]. But when I tried to use the same logic and take the values for the year in the $(this).attr('data-order', data[4]); I am getting this message

Warning: Undefined array key "year"

it is clear to me why i get that error because the year value is not placed in the post what I don't understand how to do it.

0

There are 0 best solutions below