Call function inside CodeIgniter's controller using jquery / ajax
Solution 1
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);
}
});
});
Solution 2
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')
...
});
Solution 3
I know this works. My routes file is the default.
I loaded CI URL helper in my controller __construct() function
$this->load->helper('url');
Here's my ajax:
/*
*Ajax function to load confirmation page
*/
var formID=$("div form");
formID.submit(function(event){ //activated on submit event
event.preventDefault(); //stops page from reloading
$.ajax({
type:"POST",
url:'<?php echo site_url("plan/process")?>',
data:formID.serialize(),
success:function(data){
$("div #msg_area").html(data);
window.setTimeout(function(){parent.location.reload()},3000);
}
});
});
I have multiple controllers so it calls the specific one call plan and the \n the function process. The process function one looks like this:
function process (){
$json_data = strtolower(json_encode($this->input->post()));
$res = array();
//Simple Error/success display...
$res = json_decode($this->plan->process_plan($json_data ),true);
if(array_key_exists('error',$res)){
$window = "warning";
$error=explode(":",$res['error']);
$result['message']="<h2><span class='color-dark'>Submission error:</span> ".$error[0]." </h2><p>".$error[1]."</p>";
}
else{
$window = "success";
$result['message'] = "<h2>Submission was a success</h2>";
}
echo $this->load->view("common/components/".$window,$result);
}
This works great for me. Hopefully it helps.
Solution 4
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.
Solution 5
in your routes file you have: admin321/do_search
NOT: admin/do_search
You can also try using the absolute path:
`http://www.website.com/admin/do_search` or `http://localhost/admin/do_search`
in the ajax url parameter
Comments
-
Valor_ over 3 years
Can somebody please explain to me what is the right way to call a php function with jquery / ajax in Codeigniter. Right now, this code isn't working and i cant figure out whay. Note that admin.php controler is inside admin map. Thanks in advance
html code
<form action="#" method="POST" id="change"> <input type="hidden" value="<?php echo $row->id_product; ?>" id="prod" > <input type="submit" value="switch" > </form> <div class="resultdiv"> <?php echo $data; ?> </div>
my function inside admin.php controller
public function do_search(){ $id = $this->input->post('id'); return $id; }
Jquery AJAX script
$( "#change" ).submit(function() { alert( "Change" ); var id = $('#prod').val(); $.ajax({ type:'POST', url:'admin321/do_search', data:{'id':id}, success:function(data){ $('#resultdiv').html(data); } }); });
Config / routes.php
$route['admin/do_search'] = "admin_controller/admin/do_search";
-
Valor_ about 10 yearsNop, sorry, this didn't do it.
-
Valor_ about 10 yearsI have autoladed helper url, but somehow ajax doesn't call function right or it don't retrive data right somehow..
-
Valor_ over 8 yearsGood job! This did it!
-
Brane over 8 yearsI'm glad I was able to help :)
-
natsumiyu over 4 yearswhat to do if there are parameters need in that function? how do we pass it? Thank you
-
Brane over 4 yearsYou can use data attribute as shown on the example where the ID is send to controller as JSON.