How to receive and display the data I generated from a json in a php file?

0

This is my code that generates the json

<script>
$('#table2').on('check.bs.table', function (e, row) {
//  print_r(row);
         var array = {data: row};
         var paramJSON = JSON.stringify(array);
    //  print_r(JSON.stringify(array));
        console.log(array);
          $.ajax({
         data: { data: paramJSON },
         type: "POST",
         url: 'http://localhost/correo/mail.php',
         })
         .done(function( msg ) {
         console.log(msg);

         });

and I want to receive it in this php file and show the data but it does not do anything I do not know if it is really reading the file or the data went wrong, it does not mark any error but it does not show anything.

 <?php
 if(isset($_POST["data"]))
 {
  $data = json_decode($_POST["data"]);
  var_dump($data);
  foreach($data->data as $mydata)
  {
  $idCompra = $mydata->idCompra;
  $CveSuc = $mydata->CveSuc;
  $NoOrden = $mydata->NoOrden;
  echo $idCompra." ".$CveSuc." ".$NoOrden;


  }
}

when doing a print_r this shows me

data:{  
   "data":{  
      "idCompra":"1",
      "CveSuc":"ICI",
      "NoOrden":"IC001-194",
      "NomProv":"SERVICIO PARA ESTACIONES",
      "SubtPed":"2133.9741",
      "TotalPed":"2475.41",
      "StatusPart":"Surtido",
      "FalltaPed":"2016-11-01",
      "NomUser":"5",
      "statusAut":true,
      "FechHoraAut":"2016-12-13 09:40:23"
   }
}

What I want is that with an echo show the data from the file mail.php for later that data send them by mail, the mail no problem since I use sendmail.

    
asked by sonjer 13.12.2016 в 17:01
source

2 answers

0

The problem is how the data is received from javascript in php

var row = [ 
    {cvesuc : '725226', noorden : '1234', idcompra : '1'}, 
    {cvesuc : '725666', noorden : '6789', idcompra : '2'}
];
var array = {data : row};
$.ajax({
    type: "POST",
    url: 'file.php', 
    dataType: "json",
    data : {data : JSON.stringify(array)},
    success: function(data){
        console.log(data);
    },
    error: function(data) {
        console.log(data);
    }
});

If you make a print_r(json_decode($_POST['data'])) you will notice that this comes like this:

stdClass Object
(
    [data] => Array(
        [0] => stdClass Object(
            [cvesuc] => 725226[noorden] => 1234[idcompra] => 1
        )

        [1] => stdClass Object(
            [cvesuc] => 725666[noorden] => 6789[idcompra] => 2
        )

    )
)

This is not an object that serves you for what you want, therefore you should do is set the parameter assoc of the method json_decode that is basically

  

When TRUE, the returned objects will be converted to associative array.

$array_php = json_decode($_POST['data'], true);
foreach($array_php['data'] as $mydata => $valor){
    echo $valor['noorden'];
}

IMPORTANT

When you send the array you make a JSON.stringify , if you do not do that, the json_decode is no longer necessary since for my example I ARMO from the ajax an array with JSON format, if for you it is necessary depending on As weapons your data, leave it.

$array_php = $_POST['data'];
foreach($array_php['data'] as $mydata => $valor){
    echo $valor['noorden'];
}
    
answered by 13.12.2016 / 17:46
source
0

try this, that would return in msg of the function done a json:

<?php
  if(isset($_POST["data"])) {
    $data = json_decode($_POST["data"]);
    // var_dump($data);

    foreach($data->data as $mydata) {

      $idCompra = $mydata->idCompra;
      $CveSuc = $mydata->CveSuc;
      $NoOrden = $mydata->NoOrden;
      echo $idCompra." ".$CveSuc." ".$NoOrden;

    }

    echo json_encode($data);
  }

EDITION:

See results of an ajax request, firebug in Firefox (F12):

See results of an ajax request, Google Chrome (F12):

    
answered by 13.12.2016 в 17:26