Skip to content Skip to sidebar Skip to footer

How To Pass An Array From Json Stringify Into Php Function

I am getting confused with this thing. I have an array that I have built from an HTML table as a json stringify data. I want to pass that array into the php function but with not t

Solution 1:

.table-bordered is a <table>, you cannot use jQuery's .val() function on a table element.

It makes more sense to just return the JSON string from your convertTableToJson function (like you do in your first code example) and then call it to fill the data variable:

vardata = convertTableToJson();

The data you post to the server needs to be an object with a known entry that you can access in PHP. Since you're already trying to access $_POST['table-bordered'], let's name it that:

$.ajax({
    data: {
        'table-bordered': data
    },  
    type:"POST",
    url:"../php/tagihan/save_data.php",
    dataType:"html",
        success: function(data){
            alert ("Data:" + data);
        }
});

Solution 2:

You expect a table-bordered key to be existent in $_POST, yet, it does not exist. You expect it to exist because you have a table-bordered inside the HTML, but that does not have a connection to what you passed. So, the first step is to make sure that this key will exist in your $_POST:

$.ajax({
    data:{"table-bordered": data},  
    type:"POST",
    url:"../php/tagihan/save_data.php",
    dataType:"html",
        success: function(data){
            alert ("Data:" + data);
        }
});

This way we pass data in the right way, but we need to make sure that data contains the values we need. You cannot use jQuery .val() for this purpose on a table, but I do not see the point of putting this into the HTML either. You can store it inside a variable:

$(document).on('click','#display_data',function(e){
    var rows = []; //Migrated here, as we will need it latervar convertTableToJson = function()
        {
            $('.table-bordered tr:has(td)').each(function(i, n){
                var$row = $(n);
                rows.push([
                    $row.find('td:eq(0)').text(),
                    $row.find('td:eq(1)').text(),
                    $row.find('td:eq(2)').text(),
                    $row.find('td:eq(3)').text(),
                    $row.find('td:eq(4)').text(),
                    $row.find('td:eq(5)').text(),
                    $row.find('td:eq(6)').text(),
                    $row.find('td:eq(7)').text(),
                ]);
            });
            //This line will no longer be needed//$(".table-bordered").val(JSON.stringify(table_data));
        };

    //Incorrect line//var data = $(".table-bordered").val();//Correct line, it is unnecessary though, as you can directly pass rows//as data instead, but kept this to make sure the code is close to the//initial versionvar data = rows;
    $.ajax({
        data:{"table-bordered": data},  
        type:"POST",
        url:"../php/tagihan/save_data.php",
        dataType:"html",
            success: function(data){
                alert ("Data:" + data);
            }
    });

});

Post a Comment for "How To Pass An Array From Json Stringify Into Php Function"