【问题标题】:Codeigniter validation not working with ajax but without ajax working wellCodeigniter 验证不适用于 ajax 但没有 ajax 运行良好
【发布时间】:2019-09-10 04:45:30
【问题描述】:

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

【问题讨论】:

  • 请添加console.log(contactForm) 以检查该变量中存储的内容。
  • 是的,它存储了正确的值,即使我得到成功消息响应也没有问题,但我使用 codeigniter 的验证不适用于 ajax
  • 你的 ajax 和 php 没有响应的“成功消息响应”是什么意思。此外,使用 ajax 时重定向 (redirect('Frontend/login');) 或在 php 中显示视图 ($this->login()) 是不正确的。您应该只给它一个 text/json 响应,您可以在成功/错误 js 部分中使用/解析它。如果您想以当前的方式使用它,那么您不妨完全放弃 ajax……再一次,当情况并非如此时,您将 ajax 视为普通的 php 提交。我建议您在网上查找示例。

标签: php jquery html ajax codeigniter


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这里只是概念。我没有尝试过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 中你可以将 datatype 设置为 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');
    
    
    }
    
    
                }
            });
    
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 2015-02-09
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多