Get the last id generated in the insert with jquery and redirect to another page

0

Hello, I'm having trouble getting the generated php id with jquery, so I can send this id to another page, follow the code:

queryInsert.php

$assunto = $_POST['assunto'];
$msg     = $_POST['msg'];

    $query = $db_con->prepare("INSERT INTO tb_tickets (cliente_id,ms_assunto,ms_data,ms_status,ms_read,ms_remetente,ms_destino,ms_mensagem) VALUES (:iduser,:assunto,NOW(),'0','0',:idusers,'1',:msg)");
    $query->bindParam(':iduser', $IdUsuario);
    $query->bindParam(':assunto', $assunto);
    $query->bindParam(':idusers', $IdUsuario);
    $query->bindParam(':msg', $msg);

    if($query->execute()){
        $UltId = $db_con->lastInsertId();
        echo $UltId;
    }else{
        echo 'Error ao solicitar suporte';
    }

crudTicket.js

$(function () {
    var enviandoForm = false;
    $("#emp-SaveForm").submit(function (e) {
                if (enviandoForm) {
                      return false;
                }
                this.disabled = true;
                enviandoForm = true;
                e.preventDefault();

                var FormData = $(this).serialize(); 

                $.ajax({
                  type: "POST", 
                  url: "queryInsert.php",
                  data: FormData
                 }).done(function (data) {
                    $('#emp-SaveForm').trigger("reset");
                    var notification = new NotificationFx({
                    wrapper : document.body,
                    message : ('<div class="alert alert-info">'+data+'</div>'),
                    layout : 'growl',
                    effect : 'scale',
                    type : 'notice',
                    ttl : 6000,
                    onClose : function() { return false; },
                    onOpen : function() { return false; }
                    });
                    notification.show();
                    setTimeout(function() {
                     $(".content-loader").fadeOut('slow', function()
                      {
                        $(".content-loader").fadeIn('slow');
                        $(".content-loader").load('add_ms.php?ms='+data);
                        $("#btn-add").hide();
                        $("#btn-view").show();
                       });
                      });
                    }, 1000).always(function() {
                    enviandoForm = false; //Libera o form
                    this.disabled = false;
                });

                return false;
        }); 
}); 

ms_add.php

<?php

$msGET = $_GET['ms'];

echo $msGET;

Beauty,whathappens?itregisterstheticketinthetable,ittakesthelastid,butit$(".content-loader").load('add_ms.php?ms='+data); does not work, already in this message : ('<div class="alert alert-info">'+data+'</div>') returns the normal id! Thanks in advance for your help!

    
asked by anonymous 14.02.2017 / 12:56

1 answer

3

Okay, to return, try doing this:

if ($query->execute()) {
    $UltId = $db_con->lastInsertId();
    echo json_encode(array('id' => $UltId, 'status'=>'1'));

} else {
    echo json_encode(array('id' => $UltId, 'message'=>'Error ao solicitar suporte', 'status'=>'0'));
}

And in javascript, capture this:

if (data.status == 1) {
    console.log(data.id);
} else {
    console.log(data.message);
}
    
14.02.2017 / 13:38