Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  198] [ 2]  / answers: 1 / hits: 42943  / 10 Years ago, mon, march 3, 2014, 12:00:00

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;

More From » php

 Answers
16

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);
}
});
});

[#72184] Sunday, March 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ammonderekm

Total Points: 247
Total Questions: 105
Total Answers: 98

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
ammonderekm questions
Tue, May 24, 22, 00:00, 2 Years ago
Wed, Mar 11, 20, 00:00, 4 Years ago
Thu, Dec 19, 19, 00:00, 5 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
;