Call function inside CodeIgniter's controller using jquery / ajax Call function inside CodeIgniter's controller using jquery / ajax codeigniter codeigniter

Call function inside CodeIgniter's controller using jquery / ajax


I know that this is old post, but maybe someone will find this usefull.

I solve this problem by adding index.php in url. Even if the index.php is hidden using rewrite.

  $( "#change" ).submit(function() {      alert( "Change" );      var id = $('#prod').val();         $.ajax({                type:'POST',                url:'<?php echo base_url("index.php/admin/do_search"); ?>',                data:{'id':id},                success:function(data){                    $('#resultdiv').html(data);                }            });    });


Maybe like this:

    $( "#change" ).submit(function() {  alert( "Change" );  var id = $('#prod').val();     $.ajax({            type:'POST',            url:'<?php echo base_url("admin/do_search"); ?>',            data:{'id':id},            success:function(data){                $('#resultdiv').html(data);            }        });});

You have to load this helper:

$this->load->helper('url');

@edit

$route['admin/do_search'] = "admin_controller/admin/do_search";

This code is unnecessary.


In the past, I have set up a route for the ajax request. Something like this:

$route['admin/search/(:any)'] = 'admin_controller/admin/do_search/$1';

Then my ajax request would look like this:

var prod = $('#prod').val();$.ajax({    type: 'post',    url:'admin/search/'+prod    ...});

Or, you can grab the form action via jQuery and use that as your url.

<form action="admin/search/123" method="post">$.ajax({    type: 'post',    url: $('form').attr('action')    ...});