Question

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";
Was it helpful?

Solution

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

OTHER TIPS

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top