Codeigniter验证不能与Ajax一起使用,但不能与Ajax一起很好地工作

使用Ajax Codeigniter验证提交表单时不起作用,请解决此问题,我从上周用于提交表单的jQuery代码开始遇到此问题


$(function() {

    $("#registratiom_form").on('submit', function(e) {


    e.preventDefault();

    var contactForm = $(this);

    $.ajax({

        url: contactForm.attr('action'),

        type: 'POST',

        data: contactForm.serialize(),

        success: function(response){


        }

    });


    });

});

控制器


public function add_account() {

    if($this->form_validation->run('add_account')) {

        $post = $this->input->post();

        unset($post['create_account_submit']);

        $this->load->model('Frontendmodel', 'front');

        if($this->front->add_user($post)){

            $this->session->set_flashdata('message', 'Account Created Successfully !');

            $this->session->set_flashdata('message_class', 'green');

        }

        return redirect('Frontend/login');

    } else {

        $this->login();

    }

}


FFIVE
浏览 157回答 2
2回答

拉莫斯之舞

这只是概念。我没有尝试codeigniter,但我是php专业人士。您将需要将记录检索为json并将其传递给ajax。在codeigniterheader('Content-Type: application/x-json; charset=utf-8');$result =  array("message" =>'Account Created Successfully !');              echo json_encode($result);因此代码可能如下所示public function add_account(){        if($this->form_validation->run('add_account')){            $post = $this->input->post();            unset($post['create_account_submit']);            $this->load->model('Frontendmodel', 'front');            if($this->front->add_user($post)){            header('Content-Type: application/x-json; charset=utf-8');            $result =  array("message" =>'ok');                          echo json_encode($result);                //$this->session->set_flashdata('message', 'Account Created Successfully !');                $this->session->set_flashdata('message_class', 'green');            }            return redirect('Frontend/login');        }else{            $this->login();        }    }在ajax中,您可以将数据类型设置为json,以确保可以从服务器获取响应,然后让ajax处理响应...。 $(function() {        $("#registratiom_form").on('submit', function(e) {        e.preventDefault();        var contactForm = $(this);        $.ajax({            url: contactForm.attr('action'),            type: 'POST',            dataType: 'json',            data: contactForm.serialize(),            success: function(response){            alert(response.message);            console.log(response.message);//display success message if submission is successful           if(response.message =='ok'){   alert('message submited successfully');}            }        });        });    });
打开App,查看更多内容
随时随地看视频慕课网APP