1

最初に、1 つのコントローラーから 2 つのビューをロードしようとしています。

送信時に、あるビューをあるコントローラーに渡し、別のビューを別のコントローラーに動的に 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 を送信するときです。両方のコントローラーがロードされていますが、1 つのコントローラーのみをロードする必要があります。

これが私の詳細を編集する私の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(); ?>

ここに、コントローラーとは別にロードしている commentView があります

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

誰が何が問題なのか教えてもらえますか?

4

2 に答える 2

2

私が正しく理解していれば...

キャプチャする必要があるイベントは、送信ボタンの「クリック」ではなく、フォームの「送信」です。基本的に、AJAX リクエストを実行してフォームを送信しています。

代わりに次のようにしてみてください。

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

    // do AJAX stuff
});
于 2013-02-19T07:26:21.733 に答える
0

私が間違っていない限り、イベントを「.submitComment」divのクリックイベントに添付しています。

ボタンに id を与えて .click イベントをアタッチするべきではありませんか? 次に、関数が使用できる json を返すために ajax 経由で呼び出したいビューを定義する必要があります。

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

Controller メソッド (コメント/挿入):

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

私はこのコードをテストしていないか、約 1 年間 codeigniter を広範囲に使用していませんが、ここで取り上げる概念によって、達成したいことに対して正しい軌道に乗ることができるはずです。お知らせ下さい!

于 2013-02-19T13:03:47.413 に答える