Welcome Guest, Not a member yet? Register   Sign In
How to call CI Model function from jQuery eventhandler?

Hi there,

How can I make a jQuery eventhandler call a function from a CI Model?

I would like to call the following function automatically when a user closes the browser window. (the functions saves the user's moderations)

jQuery $(window).unload seems to be the perfect eventhandler for this task.
$(window).unload( function () { /*Do_Stuff*/ } );

But how do I make this event call my CI Model function?

The models are not supposed to be called directly from a url. That is the job of the controller.

OK, but how can I let jQuery call a controller that in its turn will call the model method save()?

using it ajax functions. If you don't need feedback you can use the get or post methods, if you want more flexibility use the ajax method.
For your example it would be something like
$(window).unload( function () { $.post('index.php/controller/method',$(":input").serializeArray()) } );

OK, I'll get that working. I got lost in jQuery documentation. Thanx!

and how about if i do need feed back?,let say i'm passing form value to CI's controller and i need feed back to my previous page to let know the user that their data has been saved to DB..
$('#update').click(function() {

        var kode = $('#KODE_KK').val();
        var nama = $('#NAMA_KK').val();
        var kod = $('#kodex').val();
        var kodKK = $('#kID').val();
            alert ('Kode Kantor Masih Kosong');
        }else if(nama==''){
            alert ('Nama Kantor masih kosong');
        $.post("<?= site_url('sicon_a/kelas_kantor/update') ?>", {KODE_KK: kode,NAMA_KK: nama,kodex: kod,KODASL: kodKK},
            function() {
            $('#content').load("<?= site_url('sicon_a/kelas_kantor/view/ajax') ?>",{cache: false});

        },function() { cache: false; });
and CI controller is:
function update()
           if($_POST['KODE_KK']<>$KODKK['KODE_VAL'] OR $_POST['KODASL']==$_POST['KODE_KK'])

Theme © iAndrew 2016 - Forum software by © MyBB