How to send ID saved in variable in Json return

Asked

Viewed 101 times

1

I’m making a insert using a class but not being able to rescue the last id registered, need to save this id in a return variable json because I need it to make a new record, what I have is this:

require_once "../_classes/conexao_pdo.class.php";
require_once "../_classes/crud.class.php";

// Atribui uma conexão PDO   
$pdo = Conexao::getInstance();
$crud = Crud::getInstance($pdo, 'dvsMalote');

// INSERT
$crud = $pdo->prepare("INSERT INTO dvsMalote ( NumeroBolsa, IdUnicoopRemetente, IdUnicoopDestinatario, DataHoraEnvio, IdUsuarioEnvio, Estado  ) VALUES (?, ?, ?, ?, ?, ?)");
$crud->bindParam(1, $Bolsa, PDO::PARAM_INT);
$crud->bindParam(2, $UnicoopRemetente , PDO::PARAM_INT);
$crud->bindParam(3, $UnicoopDestinatario , PDO::PARAM_INT);
$crud->bindParam(4, $DataHoraEnvio , PDO::PARAM_STR);
$crud->bindParam(5, $Matricula , PDO::PARAM_INT);
$crud->bindParam(6, $Estado , PDO::PARAM_STR);
$retorno = $crud->execute();    

// RECUPERANDO NumeroMalote
$mensId = $pdo->lastInsertId(); 

if ($retorno):
    $retorno = array('codigo' => 1, 'mensagem' => ' Registro inserido com sucesso');
    echo json_encode($retorno);
    exit();
else:
    $retorno = array('codigo' => 0, 'mensagem' => ' Erro ao inserir o registro' );
    echo json_encode($retorno);
    exit();
endif;

I can get the id, now don’t know how to send that value to the call page, the call page waiting for the return message is like this:

if (response.codigo == "1") {
    $("#msgInsert").html('×AVISO!  ' + response.mensagem  + '');
} else {
    $("#msgInsert").html('×ATENÇÃO!  ' + response.mensagem + '');
}

How can I send this id recovered up to it and position the value in a variable?

  • 1

    In theory that’s it, you can put the method code insert()? The id is auto increment certain?

  • Hello @rray, thank you for giving me a touch on the Insert, I made an edit on my question, now I can redeem the ID but I need to send you the call page, ie in return.

  • 1

    Remember that php only sends text to javascript, so turn it into a use object var r = JSON.parse(response); Then you can do something like console.log(r.mensagem);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.