Skip to content
Advertisement

How can I get ” last inserted id to database ” in Jquery Method?

hi guys my english is a little weak . Thank you from now for help

How can I return data1 from islem.php into server.js

echo should contain ‘ok’ and $lastid

How can I get ” last inserted id to database ” in Jquery Method?

jquery codes server.js

$('#yizSub').on('click',function() {

        var yizTitle   =  $("#yizTitle").val();

        if(yizTitle.length > 1){
                        bekleyin();
                        $.post("../operation/islemmerkezi.php", {
                            tip:"yizlettir",
                            yizTitle:yizTitle,
                        },function(R){
                            if(R=="ok"){
                                Swal.fire({
                                    icon:"success",
                                    confirmButtonText: 'Tamam',
                                    title: `İşlem başarılı`
                                }).then((result) => {
                                    if (result.isConfirmed) {
                                        window.location.href = '../yizlettir.php?id=';
                                    }
                                });
                            }
                        });
                    }

    });

Php code islem.php

if($_POST['tip']=='yizlettir'){
    if(isset($_SESSION['serverOturum']) && $_SESSION['serverOturum']){
        if(isset($_POST['yizTitle']) && $_POST['yizTitle']) {

            $yizStock     = guvenlik($_POST['yizStock']);
            $yizEarn      = guvenlik($_POST['yizEarn']);
            $yizLink      = guvenlik($_POST['yizLink']);
            $yizTitle     = guvenlik($_POST['yizTitle']);
            $yizDesc      = duzenliMetin(guvenlik(mod1($_POST['yizDesc'])));
            $yizMin       = guvenlik($_POST['yizMin']);

            $ok  =   DB::insert('INSERT INTO izlekazan (user_id,url,title,description,earning,stock,min_watchtime) VALUES (?,?,?,?,?,?,?)',array(1,$yizLink,$yizTitle,$yizDesc,$yizEarn,$yizStock,$yizMin));
            $lastId = $ok->insert_id;

            if ($ok){

                $data1['msg'] = 'ok';
                $data1['last_insert_id'] = $lastId;// how can I set data1 to server.js

                echo'ok';
                return;
            }
        }

    }
}

Advertisement

Answer

In islem.php you can return the complete information as a json object:

$data1['msg'] = 'ok';
$data1['last_insert_id'] = $lastId;
echo json_encode($data1);
return

And in server.js you can use the info returned by the AJAX call like this:

var dataR = JSON.parse(R)
var msg = dataR.msg
var lastId = dataR.last_insert_id
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement