create json for post ajax to PHP

Javascript coding ..

Moderators: gesf, Michalio

Post Reply
nitiphone2021
php-forum Active User
php-forum Active User
Posts: 33
Joined: Fri Jan 08, 2021 8:10 pm

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: Select all

/* 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?
simonbrahan

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.
Post Reply