CodeIgniter Forums
loading controller via jquery - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: loading controller via jquery (/showthread.php?tid=49521)



loading controller via jquery - El Forum - 02-23-2012

[eluser]Paleleaves[/eluser]
Hi,

My issues is with jquery and codeigniter. That's why posted here...

I've an image delete confirmation dialog box in jquery, that is below. Receiving ID of the image to be deleted in var myid. Please see the full code below!

Code:
[removed]
$(document).ready(function(){

$('.delete').click(function(){
  
var myid = $(this).attr("id");

$.confirm({
   'title'  : 'Delete Confirmation',
   'message' : 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
   'buttons' : {
    'Yes' : {
     'class' : 'blue',
     'action': function(){
      
      
// I want to call the controller here. This JS file included as a separate file.  
            
     }
    },
    'No' : {
     'class' : 'gray',
     'action': function(){
      
     } // Nothing to do in this case. You can as well omit the action property.
    }
   }
  });
  
});

});
[removed]

Please see the commented area in code where i need to call the controller. I tried jquery $.post() method but didn't workout. Also another problem is setting the site base_url(); inside the javascript. Any help for this really helpful.

Thanks in advance


loading controller via jquery - El Forum - 02-23-2012

[eluser]aquary[/eluser]
uh.... was typing a very long comment and accidentally hit something on keyboard.... making everything disappear...

k, in short... you can pull the URL directly from the trigger elements...

Code:
// PHP/HTML
<a href="&lt;?=site_url('controller/delete/'.$id);?&gt;" class="delete">Delete</a>

// JS
$(".delete").click(function(ev){
    var url=$(this).attr('href'); // jQuery way
    var url=ev.target.href; // Native javascript, faster
});

You could also define the base_url in javascript, but it's not 100% bullet proof with out some manual editting.
Code:
var base_url=wind0w.l0cation.hostname; // change 0 to o
That will give you only the hostname, so If your code is running in some folder, or with index.php, you'll have to add them manually.

Then about the action part, non AJAX wise, you may return TRUE/FALSE for Yes/No accordingly to continue the click event or stop it.

and about the $.post which you said not working... maybe it was about the URL which is not a good one, but with the above method, you'll always get a good one....

I suggest you to check what are the URL called in the Firebug/Chrome console, to see If it's a good URL or with some errors, either in PHP or JS.

grr... getting headache by typing the same stuff 2 times .-.



loading controller via jquery - El Forum - 02-23-2012

[eluser]Paleleaves[/eluser]
@aquary

Thank you very much for your patience to sent such a detailed comment. My issue is resolved now..

Thanks again for you .. cheers Smile