create json for post ajax to PHP

nitiphone2021

New member
I have a dynamic form want to send the information to PHP side by AJAX, so I think it will be good if sending by JSON format
This is my code
Code:
/* This is get row count from table*/
var rowlength = document.getElementById("sublab").rows.length;

/* I create array to get information*/
    var sublabdata = [];
/* push information of input to the array*/
    for(var i = 1;i <= (parseInt(rowlength) - 1);i++){
        sublabdata.push([document.getElementById("sub_task" + i).value,document.getElementById("sub_lab" + i).value]);
    }
/* Send the information to php by json*/
    $.ajax({
        url: "action.php",
        method: "POST",
        data: { function : 'updatesublab',
            updatesublab : JSON.stringify(Object.assign({}, sublabdata)),
        },
        success: function (result) {
               

            
    },

From by code, it can send to PHP but it's very hard for using the information on PHP
Could you please kindly guide me how to crate json for sending to PHP?
 
You can use the PHP function json_decode to convert the input to a PHP object or array. That should be easier to work with.
 
Back
Top