当我退出屏幕时,我会被问到:“你要保存吗?”codeigniter

I want to do something like this: If I am viewing a record and edit it or add a new record and then exit that screen, I should be asked, “Do you want to save?”

How can I develop this thing in php? I haven't done this type of development before..

Any suggestions?

use jquery onbeforeunload function it will be execute when page refresh or closing browser tab or closing browser .

 $(window).on('beforeunload', function(){
                  return 'Are you sure you want to leave?';
           });

</div>

You need to look for changes in javascript. Make a global variable, lets call it changed . Set it to false on page load. Whenever record is edited make it true and whenever it is saved , set it to false. When the user is closing his tab, again which needs to be detected in JS, look for changed variable. If it is true give him a prompt else he can close without any issues.

You will need to javascipt/jquery to listen for the unload event on the window.

I believe you could do something along the lines of:

<script>
function saveAlert() {
    var confirm = confirm('You haven\'t saved your form! Do you want to save?');

    if (confirm) {
        $('form').submit();
    }

    return confirm;
}

$(function() {
    var formSaved = <?=($formSaved) ? 'true' : 'false'?>;

    if (!formSaved) {
        $( window ).unload(saveAlert());
    }
});

All you will need to do is pass a boolean in the $formSaved variable to determine whether the alert needs to be shown or not.

If you wanted to attempt a solution to this question in PHP, you would need to use AJAX to store field data in the database on change/update of fields, without committing the change to the table - you could store it in a cookie or session variable, or in an 'unsaved_records' table of some sort.

If your user navigates to an off-site domain there's nothing you can do in PHP but if they come back to your site, you can alert "you have unsaved data, do you want to continue where you left off?".

You could also wait till they return to the page where they had unsaved data, and restore it to the state it was in, as though they had never left. This would require some careful planning, but it's possible.

Only client side scripting can pause the window or tab being directed to a new location, as is evident in the other answers here.