jquery - 在当前弹出窗口中显示错误消息(从数据库获取记录)

标签 jquery ajax codeigniter

我想在当前弹出页面进入otp页面弹出窗口之前检查手机号码是否已注册。

我的 Controller

        public function VerificationOTP() {
        $postData = $this->input->post();
        if (isset($postData) && !empty($postData)) {
            $this->load->model('DoctorSearch');

            $exest = $this->DoctorSearch->checkPhoneNumber($postData,3);
            $data['mobileNumber'] = $this->input->post('mobileNumber');
            $data['loginOTP'] = ($this->input->post('loginOTP'))?$this->input->post('loginOTP'):'0';
            if ($exest['success']) {
                $data['mobilePrefix'] = $exest['mobilePrefix'];
                     $this->session->set_userdata('patmob',$exest['mobileNumber']);
                $this->session->set_userdata('patmobp',$exest['mobilePrefix']);
                $this->session->set_userdata('paturef',$exest['userRef']);

                $this->load->view('pages/otp',$data);

            } else {
        $msg = '<div class="alert alert-danger">';
                $msg .= 'Please Enter Registered Mobile Number';
                $msg .= '</div>';
                $this->session->set_flashdata("message", $msg);

                //$this->session->set_flashdata("message", '');
                redirect('verify-mobilenumber');
            }
        }

}

My script in view page;


              $(document).ready(function () {
            $("#submit").click(function () {
        var mobileNumber = $("#mobileNumber").val();
        $("form[name='mobnumber']").validate({
            rules: {
                mobileNumber: {
                    required: true
                },
            },
            messages: {
                mobileNumber: {
                    required: "Enter Mobile Number"
                },
            },
            submitHandler: function (form) {
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url('verify-mobilenumber'); ?>",
                    data: {mobileNumber: mobileNumber},
                    success: function (result) {
                    if(result){
                     $("#enter-otp").modal('show');
                     $("#ver-num").html(mobileNumber);

                    }
                    }
                });
            }
        });

    });

});

在给定注册号码之前,我不想打开#enter-otp 弹出窗口。我想显示 Controller $msg .= '请输入注册的手机号码';在我当前的弹出窗口中作为失败消息

最佳答案

您需要像这样在 $.ajax 提交中添加 dataType

在您的 View 文件中

    <div class="modal fade" id="enter-otp" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Error</h4>
            </div>
            <div id="OptBody" class="text-center">
            </div>
        </div>
    </div>
</div>
    <script>
    $.ajax({
           type: "POST",
           url: "<?php echo base_url('verify-mobilenumber'); ?>",
           data: {mobileNumber: mobileNumber},
           dataType: "json",
           success: function (result) {
             if(result.errors){
                $("#enter-otp").modal('show');
                $('#OptBody').append(result.errors);
                $("#ver-num").html(mobileNumber);
             }
           }
        });
    </script>

在您的 Controller 文件中,创建一个空数组来保存您想要返回的数据,并执行 json_encode 以便能够将其返回。

public function VerificationOTP() 
{
    $data = array('errors' => NULL);
    $postData = $this->input->post();
    if (isset($postData) && !empty($postData)) {
        $this->load->model('DoctorSearch');

        $exest = $this->DoctorSearch->checkPhoneNumber($postData,3);
        $data['mobileNumber'] = $this->input->post('mobileNumber');
        $data['loginOTP'] = ($this->input->post('loginOTP'))?$this->input->post('loginOTP'):'0';

        if ($exest['success']) {
            $data['mobilePrefix'] = $exest['mobilePrefix'];
            $this->session->set_userdata('patmob',$exest['mobileNumber']);
            $this->session->set_userdata('patmobp',$exest['mobilePrefix']);
            $this->session->set_userdata('paturef',$exest['userRef']);

            $this->load->view('pages/otp',$data);

        } else {
            $data['errors'] = "<p>Please Enter Registered Mobile Number</p>";
            //$this->session->set_flashdata("message", '');
            //redirect('verify-mobilenumber');
        }
    }
    echo json_encode($data);
}

关于jquery - 在当前弹出窗口中显示错误消息(从数据库获取记录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49670105/

相关文章:

javascript - 重置表格中所有输入的默认值(不包括表格)

javascript - 山魈 "reject_reason":"unsigned"

javascript - Ajax .load 从服务器检索内容

jquery - deviantart 如何根据浏览器窗口的宽度在页面上动态添加新内容?

javascript - 许多列的数据表宽度溢出

jquery - 通过 JQuery .val() 将多个数据字段从 <select multiple> 传递到 Django formfield

php - CI : Master Detail for User Registration Form doesn't work

php - 如何在 CodeIgniter 验证后重定向到 View 页面中的 anchor 标记

javascript - 用于 AngularJS 的 jQuery "$.when().done()"

php - Doctrine 多对多插入