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);
    }`
8
  • Please post your code so that we can have a look Commented May 9, 2017 at 13:44
  • Have you written the AJAX code? It's pretty simple to send JSON over AJAX with jQuery. See jQuery AJAX docs here. Commented May 9, 2017 at 13:45
  • 2
    Presumably obj is defined and has the expected value? Commented May 9, 2017 at 13:49
  • Properties should not begin with a number as they aren't valid in most languages. Commented May 9, 2017 at 13:49
  • 2
    Can you provide some sample HTML too. Ideally a jsFiddle showing the issue. Commented May 9, 2017 at 14:03

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You found the solution. No problems with that. Good job.
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"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.