2013-02-19 129 views
0

最初我试图从一个控制器加载两个视图。Codeigniter - 使用ajax从视图中调用另一个控制器

在提交时,我将一个视图传递给一个控制器,另一个视图动态传递给另一个使用ajax的控制器。这里是控制器的骨架

function edit(){  
    if (!$this->login_model->is_logged_in()) 
    { 
     redirect('auth/login', 'refresh'); 
    } 
    $this->load->library('form_validation');  
    $this->data['custom_error'] = ''; 

     //------------- Getting Student data---------------- 

    if ($this->form_validation->run('students') == false) 
    { 
     $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false); 

    } else 
    {        
     $data = array(
       'username' => $this->input->post('username'), 
       'city' => $this->input->post('city') 
     ); 

     if ($this->student_model->edit('students',$data,'id',$this->input->post('id')) == TRUE) 
     { 
      redirect(base_url().'index.php/students/manage/'); 
     } 
     else 
     { 
      $this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>'; 

     } 
    } 

    $this->data['result'] = $this->codegen_model->get('students','id,username,city,created_on,date_created','id = '.$this->uri->segment(3),NULL,NULL,true); 

    $this->data['message'] = $this->db->get('messages')->result(); 

      //---------------------- Posting student data for Edit-------------- 
    $this->load->view('pheader'); 
    $this->load->view('/students/students_edit', $this->data); 

      //-------- loading comment controller for comment box -------------- 
      $msg_data['result'] = $this->db->get('messages')->result(); 
      $this->load->view('commentView', $msg_data); 
} 

所以的问题是,当我米提交两个控制器装载的messages_view,但我需要加载只有一个控制器

这里是我的student_edit图,其中编辑我的信息

<?php  

echo form_open(current_url()); ?> 
<?php echo $custom_error; ?> 
<?php echo form_hidden('id',$result->id) ?> 

     <p><label for="username">Username<span class="required">*</span></label>         
     <input id="username" type="text" name="username" value="<?php echo $result->username ?>" /> 
     <?php echo form_error('username','<div>','</div>'); ?> 
     </p> 

     <p><label for="city">City<span class="required">*</span></label>         
     <input id="city" type="text" name="city" value="<?php echo $result->city ?>" /> 
     <?php echo form_error('city','<div>','</div>'); ?> 
     </p> 
     <?php echo form_submit('submit', 'Submit'); ?> 
</p> 

<?php echo form_close(); ?> 

下面是我分别从控制器加载评论查看

<div id="content-table-inner"> 
    <table border="0" width="100%" cellpadding="0" cellspacing="0"> 
     <?php foreach ($result as $comment): ?> 

      <tr valign="top"> 
       <p>Posted By : <?=$comment->created_by?></p> 
       <p>Posted On : <?=$comment->created->on?></p> 
       <p>Message : <?=$comment->message?></p> 
      </tr> 
      <br/> 
     <?php endforeach; ?> 
     </table> 
     <div class="submitComment" id="insertbeforMe"> 
      <h3>Add a message</h3> 
      <form id="form" method="POST" action=""> 
      <p> 
       <textarea name="message"></textarea> 
      </p> 
       <input type="hidden" value="<?=base_url()?>" id="baseurl"/> 
       <button name="submit comment">Submit Comment</button> 
      </form> 
     </div> 
     <script type="text/javascript"> 
     function comment(){ 
      var baseurl = $('#baseurl').val(); 
      $('.submitComment').click(function(){ 
       $.ajax({ 
        url : baseurl + 'index.php/comment/insert', 
        data : $('form').serialize(), 
        type: "POST", 
        success : function(comment){ 
         $(comment).hide().insertBefore('#insertbeforMe').slideDown('slow'); 
        } 
       }) 
       return false; 
      }) 
     } 
     </script> 

</div> 

谁能告诉我最新的问题?

+0

为什么不改变ajax来调用控制器呢? – ajreal 2013-02-19 07:23:46

+0

@ajreal:我将有两个表单提交,那么我怎样才能在一个网页中向两个不同的控制器提交两种不同的表单? – Neal 2013-02-19 11:09:47

+0

这两个请求可以合并为一个 – ajreal 2013-02-19 12:22:33

回答

2

如果我理解正确的...

你应该捕捉事件是窗体的“提交”,而不是提交按钮的“点击”。您基本上正在运行提交表单的AJAX请求

尝试这样代替:

$('form').on('submit', function(e) { 
    e.preventDefault(); // this prevents the form from submitting 

    // do AJAX stuff 
}); 
+0

它不工作.. – Neal 2013-02-19 11:12:13

0

除非我弄错要附加上“.submitComment” DIV事件的单击事件。

如果你不给该按钮一个ID并附加.click事件给它? 然后,你需要定义你想通过AJAX调用视图返回一些JSON的功能可以使用的:

的JavaScript:

 $('#button').click(function(){ 
        $.ajax({ 
         url : baseurl + 'index.php/comment/insert', 
         data : $('form').serialize(), 
         type: "POST", 
         dataType: "json", 
         success : function(comment){ 
          $('table').append('<tr><p>'+comment.createdby+'</p><p>'+comment.createdon+'</p><p>'+comment.message+'</p></tr>'); 
         } 
        }) 
        return false; 
     }) 

的控制方法(评论/插入):

function insert() { 
    $comment = $this->input->post('message'); 
    $result = $this->commentModel->insert($comment); 
    if($result["OK"]) { 
     $data['json'] = json_encode(array('createdon' => $result["createdon"], 'createdby' => $result["createdby"], 'message' => $result["message"])); 
     $this->load->view('json_comment_view', $data); 
    } 
    //ELSE deal with errors (flashdata etc...) 
} 

的视图(通过AJAX加载):

<?php 
    $this->output->set_header('Content-Type: application/json; charset=utf-8'); 
    echo $json; 
?> 

我还没有测试过这个代码 - 或者在一年内广泛使用了代码语言,但是所涵盖的概念应该能够让您找到想要实现的目标。让我知道!

+0

仍然没有得到它作为以前的形式仍然抛出闪存错误 – Neal 2013-02-19 19:17:34

+0

你能澄清?什么以前的形式?另外 - 你准确收到了什么错误?谢谢。 – 2013-02-19 19:20:26

+0

像我有两个意见edit_person_details_view和comments_view来自不同的控制器,但显示在同一个网页上。我需要提交两个分开发生。我怎样才能做到这一点 ? – Neal 2013-02-19 19:27:14

相关问题