Take the last id generated in the Insert with jquery and redirect to another page

Asked

Viewed 151 times

0

hello, I’m having a hard time getting the id generated in php 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;

Aparece o id e btn-view, porem não aparece o echo... All right, what happens? he registers the ticket in the table, he gets the last id, but he gets this guy $(".content-loader").load('add_ms.php?ms='+data); does not work, already in that message : ('<div class="alert alert-info">'+data+'</div>') return me the normal id! thanks for the help!

  • Makes a console.log(data); right at the beginning of your done posted what appears

  • No page is loaded on .content-loader or the page add_ms.php is loaded, only without the correct value of ms?

  • @Thiagosantos, the id usually appears, as the path said in the question!

  • then @Andersoncarloswoss, btn-add some and btn-view appears, for echo $_GET['ms']; it returns me nothing, that echo is at the beginning of the page the rest of the page has some tables and etc, which also do not appear!

  • So apparently it’s a request flaw and not the value of the query string. Your server shows some 404 error in log to the archive add_ms.php?

  • no... look at the page img in the question, simply blank!

  • Ok, you edited by putting the file name as ms_add.php but the load is from the archive add_ms.php. The mistake is not that or just got confused when posting here?

  • He cited the file name as ms_add.php and in the .load() placed add_ms.php, it was just a clerical error or so in the file name and code?

  • error writing sorry! error persists!

  • testing with window.location.href='teste2.php? ms='+data; it works! but I really need load(). teste2.php was a page I created and only put echo $_GET['ms'];

  • 1

    I thought you were the .load(), I don’t know if he interprets the php file using this method. Therefore, it should not be bringing results. Try to turn the return result into json.

Show 6 more comments

1 answer

3


Okay, to return, try 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 like this:

if (data.status == 1) {
    console.log(data.id);
} else {
    console.log(data.message);
}
  • I can already get the id with $db_con->lastInsertId();, the problem is redirecting!

  • If you had used the API mysqli, yes, but I believe he’s using PDO, since it is bindParam in query, nay bind_param, as it would be in the mysqli.

  • Thanks guys, thanks so much for the help, I added dataType json... and did what @Ivan published and it worked perfectly! but a simple question because it only worked with json, that is, with the data.id?

  • Dude... I don’t know for sure... but I think when you called the page using the .load(), php code was not interpreted by the server. That is, say you were trying to make a include from a php file without the server interpreting the code.

Browser other questions tagged

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