Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在CodeIgniter中使用AJAX检索多个对象?_Php_Jquery_Ajax_Codeigniter - Fatal编程技术网

Php 如何在CodeIgniter中使用AJAX检索多个对象?

Php 如何在CodeIgniter中使用AJAX检索多个对象?,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我正在我的网站上准备一个“管理员”注释表。基本上,我想做两件事: 使用AJAX将新注释插入数据库中的“notes”表中 如果插入成功,请使用AJAX更新页面上显示的notes表 - 如果$data['is_success']为真,我想执行另一个AJAX请求: $.ajax({ url: "<?php echo base_url();?>admin/get_notes_table", type: 'POST', success: func

我正在我的网站上准备一个“管理员”注释表。基本上,我想做两件事:

  • 使用AJAX将新注释插入数据库中的“notes”表中
  • 如果插入成功,请使用AJAX更新页面上显示的notes表
  • -

    如果$data['is_success']为真,我想执行另一个AJAX请求:

    $.ajax({
            url: "<?php echo base_url();?>admin/get_notes_table",
            type: 'POST',
            success: function(table) 
            {
                $('#wrap').html(table);
            }
    });
    
    $.ajax({
    url:“管理/获取注释表”,
    键入:“POST”,
    成功:功能(表)
    {
    $('#wrap').html(表格);
    }
    });
    
    我怎样才能完成这个?我尝试将第二个请求放在第一个请求的内部和之后,但我仍然需要知道插入是否成功


    我刚刚开始在jQuery中学习CI和AJAX,非常感谢您的帮助。谢谢

    您只能在一个ajax调用中执行此操作,只需更改add_note controller函数, 而不是在视图中返回true/false,它应该返回json。 此json将包含success或false以及新的注释列表。您可以在success函数中解析此json以显示注释

        public function add_note() {
            $note = $this->input->post('note');
            $type = $this->input->post('type');
    
            $data['is_success'] = $this->model_admin->add_note($note, $type);
            if($data['is_success']){
              //get the new notes from the db including the new one
              $data['result']     = "success";
              $data['motes'] = notes from DB
            }else{
              $data['result'] = "fail";
            }
    
            echo json_encode($data);exit;
    
    } 
    

    它的好处是,您的一个ajax调用被保存,这意味着用户exp会更好,因为他不必再等待了。

    尝试json,那么您就不需要第二次请求。还不太熟悉json,但我会看看。
    $.ajax({
            url: "<?php echo base_url();?>admin/get_notes_table",
            type: 'POST',
            success: function(table) 
            {
                $('#wrap').html(table);
            }
    });
    
        public function add_note() {
            $note = $this->input->post('note');
            $type = $this->input->post('type');
    
            $data['is_success'] = $this->model_admin->add_note($note, $type);
            if($data['is_success']){
              //get the new notes from the db including the new one
              $data['result']     = "success";
              $data['motes'] = notes from DB
            }else{
              $data['result'] = "fail";
            }
    
            echo json_encode($data);exit;
    
    }