繁体   English   中英

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

[英]Call function inside CodeIgniter's controller using jquery / ajax

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

HTML代码

<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>

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

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

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

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

我知道这是旧帖子,但也许有人会觉得这很有用。

我通过在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);
                }
            });
    });

也许是这样的:

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

你必须加载这个帮手:

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

@编辑

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

这段代码是不必要的。

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

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

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

这是我的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);
        } 
    });
});

我有多个控制器,因此它调用特定的一个呼叫计划和\\ 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);


    } 

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

在您的路线文件中: admin321/do_search NOT: admin/do_search

您也可以尝试使用绝对路径:

`http://www.website.com/admin/do_search` or `http://localhost/admin/do_search`

在ajax url参数中

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM