Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
            }
        }

    }
}

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading