使用jquery / ajax在CodeIgniter控制器内调用函数

时间:2022-10-06 19:20:59

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

有人可以向我解释在Codeigniter中用jquery / ajax调用php函数的正确方法是什么。现在,这段代码不起作用,我无法弄清楚。请注意,admin.php控制器位于管理映射中。提前致谢

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

我的函数在admin.php控制器里面

 public function do_search(){
        $id = $this->input->post('id');
        return $id;
    }

Jquery AJAX script

Jquery AJAX脚本

$( "#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

Config / routes.php

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

5 个解决方案

#1


5  

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.

我通过在url中添加index.php来解决这个问题。即使使用重写隐藏了index.php。

  $( "#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);
                }
            });
    });

#2


2  

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.

这段代码是不必要的。

#3


2  

I know this works. My routes file is the default.

我知道这很有效。我的路线文件是默认的。

I loaded CI URL helper in my controller __construct() function

我在我的控制器__construct()函数中加载了CI URL帮助器

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

Here's my ajax:

这是我的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:

我有多个控制器,因此它调用特定的一个呼叫计划和\ n功能过程。过程函数看起来像这样:

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.

这对我很有用。希望它有所帮助。

#4


1  

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

过去,我已经为ajax请求设置了一条路线。像这样的东西:

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

Then my ajax request would look like this:

然后我的ajax请求看起来像这样:

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.

或者,您可以通过jQuery获取表单操作并将其用作您的URL。

<form action="admin/search/123" method="post">

$.ajax({
    type: 'post',
    url: $('form').attr('action')
    ...
});

#5


1  

in your routes file you have: admin321/do_search NOT: admin/do_search

在你的路线文件中你有: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

在ajax url参数中

#1


5  

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.

我通过在url中添加index.php来解决这个问题。即使使用重写隐藏了index.php。

  $( "#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);
                }
            });
    });

#2


2  

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.

这段代码是不必要的。

#3


2  

I know this works. My routes file is the default.

我知道这很有效。我的路线文件是默认的。

I loaded CI URL helper in my controller __construct() function

我在我的控制器__construct()函数中加载了CI URL帮助器

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

Here's my ajax:

这是我的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:

我有多个控制器,因此它调用特定的一个呼叫计划和\ n功能过程。过程函数看起来像这样:

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.

这对我很有用。希望它有所帮助。

#4


1  

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

过去,我已经为ajax请求设置了一条路线。像这样的东西:

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

Then my ajax request would look like this:

然后我的ajax请求看起来像这样:

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.

或者,您可以通过jQuery获取表单操作并将其用作您的URL。

<form action="admin/search/123" method="post">

$.ajax({
    type: 'post',
    url: $('form').attr('action')
    ...
});

#5


1  

in your routes file you have: admin321/do_search NOT: admin/do_search

在你的路线文件中你有: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

在ajax url参数中