Codeigniter验证不适用于ajax,但不能与ajax一起正常工作

时间:2019-04-20 02:56:52

标签: php jquery html ajax codeigniter

使用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();
    }
}

2 个答案:

答案 0 :(得分:0)

这里仅仅是概念。我没有尝试codeigniter,但我是php专业人士。

您将需要检索记录为json并将其传递给ajax。在codeigniter

header('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');


}


            }
        });

        });
    });

答案 1 :(得分:0)

您对ajax响应者可以做什么和不能做什么有误解。它不能做的一件事就是使用PHP使浏览器重定向到新页面。您必须将线索发送回success函数,然后做出适当的反应。

对@Nancy的答案进行了一些细微更改,您应该会很好。

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');
            echo json_encode(array("result" => 'ok'));
            return;
        }
        $message = '<span class="error">Account Not Created!</span>';
    }
    else
    {
        $message = validation_errors('<span class="error">', '</span>');
    }
    echo json_encode(array("result" => 'invalid', 'message' => $message));
}

在Javascript中,使用$ .ajax的success函数处理各种响应

$(function () {
        $("#registratiom_form").on('submit', function (e) {
        var contactForm = $(this);
        e.preventDefault();

        $.ajax({
            url: contactForm.attr('action'),
            type: 'POST',
            dataType: 'json',
            data: contactForm.serialize(),

            success: function (response) {
                console.log(response); // so you can examine what was "echo"ed from the server

                if (response.message=='ok') {
                     // Simulate an HTTP redirect: to the right page after successful login
                     window.location.replace( "https://example.com/frontend/somepage");
                } else {
                    //stay on the same page but show the message in some predefined spot
                    $('#message').html(response.message);
                }
            }
        });
    });
});