0

I have this Problem and because I am very fresh in this subject I don't know how to procedure. I want to read the Content of a Array variable sended from Jquery by Post to the Server Php. Then I could separate the values and send differents query to the DB

This is what I have:

var Content = {};
var subcontent= {};
var key;

$("table tr.data").each(function(i) {
   var row = [];
   key = $(this).find('td').eq(0).find('input').val();

   row.push($(this).find('td').eq(1).text());
   row.push($(this).find('td').eq(2).text());
   row.push($(this).find('td').eq(5).text());

   subcontent[key] = row;

});

Content['.select'.val()] = subcontent;

var ContentJSON= JSON.stringify(Content);

$.ajax({
       data: ContentJSON,
       url:   'page.php',
       type:  'POST',
       dataType: 'json',
       beforeSend: function (){
                alert('Information wird gespeichert');
       },
       success:  function (r) {
                $("#resultado").html(r);
                alert('Information wurde gespeichert');
       },
       error: function(){
              alert('Fehler ..');
       }
        });

How is the procedure in Server side with PHP to read the Content of this variable. Can please somebody help me?

zgrizzly_7
  • 773
  • 2
  • 10
  • 22

2 Answers2

0

You need to use this :

$post = file_get_contents('php://input');

You will get the content of your JSON.

Check this answer for more details : Get all variables sent with POST?

Community
  • 1
  • 1
QuentinG
  • 101
  • 2
  • 11
0

Not quite sure what it is your asking. I guess you're sending the Json data to the file page.php? Could you show the code for the page.php file?

Also, please not ethat success and error are deprecated as of jQuery 1.8, I would suggest the .done(), .fail() and .always() callbacks instead, implementing the $.post() method

$.post( 'page.php' )
  .done(function(data) {
    alert( "success" );
  })
  .fail(function(data) {
    alert( "error" );
  })
  .always(function(data) {
    alert( "complete" );
  });

You can handle the data that gets returned from your page.php file if you pass it in the callback.

If you show me what's happening server side, it would help me understand.

Michael

Michael Emerson
  • 1,774
  • 4
  • 31
  • 71
  • Hi Michael! Thx for your answer! Here is the simple code of my php file. I don't know what I am doing wrong. If you could help me will be great. – zgrizzly_7 Sep 04 '15 at 21:15
  • OK, I see your issue. In your php file you are trying to send back the post value of 'ContentJSON' but this doesn't exist - you are sending this via the data property of your ajax function. So you need to post back using $_POST['data'] in order to get the correct data. Also if you are wanting to return using the print_r() function you need to use true as the second parameter (the return type) check http://php.net/manual/en/function.print-r.php to see how this works. – Michael Emerson Sep 06 '15 at 13:31