0

I have reached here after doing a lot research, Its simple task creating a lot heck I am unable to pass javascript array to php some links which i viewed 1, 2, 3

I have created javascript object

obj={};

//for each loop of table to create the key - value pair of object

obj[$(r).find('td:eq(2)').html()]=i+1;

//end of for each loop

//now I console the object which shows in console like this console image of object

after doing this var myJSON =JSON.stringify(obj);

I get the result as empty json string,I want to pass this sting to php via ajax, Not able to figure out why I have tried creating array as well, need help

enter image description here

fiddle link here here is full code

    $fields_table=$('#datatableRank');
    obj={};
    table.on( 'row-reorder', function ( e, diff, edit ) {

    setTimeout(function() {
    // Update the field order values
    $fields_table.find('tbody').find('tr').each(function(i,r){
            //var element = {};
            //element.id = $(r).find('td:eq(2)').html();
            //element.quantity = i+1;
            //obj.push(i+1);
            //obj1.push($(r).find('td:eq(2)').html());
            $(r).find('td:eq(0)').html(i+1);
            obj[$(r).find('td:eq(2)').html()]=i+1;
        });
    }, 10); // Give it time to load in the DOM
    console.log(obj);
    var myJSON =JSON.stringify(obj);
    console.log(myJSON);
    }`
Community
  • 1
  • 1
dEL
  • 482
  • 1
  • 6
  • 23

2 Answers2

1

turning out to be another rat in pipe everything was perfect only issue was with setTimeout function() I had to console the code in settimeout function , now I wrote the ajax code inside the settimeout and thats it pretty dumb I am

dEL
  • 482
  • 1
  • 6
  • 23
0

You can post data to php file , jquery post

like this

$.post( "yourfile.php", { data: myJSON })
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

then get it from php file "yourfile.php"

json_decode($_POST["data"])