Codeigniter验证不使用ajax但没有ajax运行良好

when submitting the form using ajax codeigniter validation not working please resolve this issue i am facing this problem from last week jQuery code that i am using for submitting form

$(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){

        }
    });

    });
});

Controller

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

Here is just the concept. I have not tried codeigniter but am php professional.

You will need to retrieve records as json and pass it to ajax. At codeigniter

header('Content-Type: application/x-json; charset=utf-8');
$result =  array("message" =>'Account Created Successfully !');              
echo json_encode($result);

hence the code might look like below

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

in ajax you can set datatype to json to ensure that you can get response from the server and then let ajax handle the response....

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


}


            }
        });

        });
    });

You have a misunderstanding about what an ajax responder can and cannot do. One thing it cannot do is use PHP to make the browser redirect to a new page. You will have to send a clue back to the success function and then react appropriately.

A few minor changes to the answer from @Nancy and you should be good.

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

In the Javascript, handle the various responses in the success function of $.ajax

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