process data from php-mysql to json

0

I have a series of php, ajax and js files to be able to process my information obtained from my database (MySQL) and at the same time I want to send them to a modal using a .js

So far I can get my result from my query using a var_dump:

This is the code of my query, I want to process it to json to later send it from a js.

 <?php 

$id_sol = $_POST['id'];
$solicitud = SolicitudData::getTraslateAlmacen($id_sol);
echo json_encode($solicitud);
var_dump($solicitud);

?>

This my .js

function obtenerDatos(id){
$.post("index.php?action=getTraslateAlmacen", { id : id }, function( data ){
    var data = JSON.parse(data);
    $("#process_id").val(data.id);
    $("#folio").val(data.folio_solicitud);
});

}

Investigating what I need is converting my php query to json objects but I can not get it.

    
asked by Ever 05.01.2018 в 15:53
source

2 answers

1

I found a solution, thanks for your contributions, I share it in case someone else has something similar:

    <?php

$id_sol = $_POST['id'];

$solicitud = SolicitudData::getTraslateAlmacen($id_sol); 

foreach ($solicitud as $key) {

    $response[0] = [
                    "id"=>$key->id,
                    "folio"=>$key->folio,
                    "pedimento"=>$key->ped,
                    "nombre_solicitante"=>utf8_decode($key->nombre),
                    "cliente_id"=>$key->cliente_id
                    ];

    echo json_encode($response);
}

?>
    
answered by 17.01.2018 / 16:23
source
0

Your code does not work for the var_dump, so you do not make any parse in javascript you must send the correct headers the code would be like this:

<?php 
    $id_sol = $_POST['id'];
    $solicitud = SolicitudData::getTraslateAlmacen($id_sol);
    header("content-type: application/json");    
    echo json_encode($solicitud);

And in javascript it would be like this

function obtenerDatos(id){
$.post("index.php?action=getTraslateAlmacen", { id : id }, function( data ){
    if(typeof data !='object'){
       alert('hubo un error en la respuesta del servidor');
       return;
    }
    $("#process_id").val(data.id);
    $("#folio").val(data.folio_solicitud);
});
    
answered by 05.01.2018 в 18:02