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

[eluser]JoostV[/eluser]
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)
Code:
$this->articles->save($id);

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

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

[eluser]xwero[/eluser]
The models are not supposed to be called directly from a url. That is the job of the controller.
#3

[eluser]JoostV[/eluser]
OK, but how can I let jQuery call a controller that in its turn will call the model method save()?
#4

[eluser]xwero[/eluser]
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
Code:
$(window).unload( function () { $.post('index.php/controller/method',$(":input").serializeArray()) } );
#5

[eluser]JoostV[/eluser]
OK, I'll get that working. I got lost in jQuery documentation. Thanx!
#6

[eluser]doni[/eluser]
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..
Code:
$('#update').click(function() {

        var kode = $('#KODE_KK').val();
        var nama = $('#NAMA_KK').val();
        var kod = $('#kodex').val();
        var kodKK = $('#kID').val();
    
        if(kode=='')
        {
            alert ('Kode Kantor Masih Kosong');
            return(false);
        }else if(nama==''){
            alert ('Nama Kantor masih kosong');
            return(false);
        }else{
        $.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});
            $('#KODE_KK').val('');
            $('#NAMA_KK').val('');

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




Theme © iAndrew 2016 - Forum software by © MyBB