发送错误消息以使用codeigniter

时间:2016-11-03 05:00:51

标签: javascript jquery codeigniter post

我有一个提交email的表单,如果提交的电子邮件未经过验证(状态= 1),则表示条件无法插入user_driver表。我想要实现的是如何显示错误消息,例如"请验证您的电子邮件"当用户在填写电子邮件字段后点击add按钮?我正在使用jquery将输入发送到控制器,我的表单在modal引导程序内。即使未满足要求(success),然后email_verified = 1页面,它也会点击reload功能。所以,我有点混淆如何显示错误信息。

我的观点:



function add_driver()
{
    user_email = $("#user_email").val();
    $.ajax
    ({
            url : site_url+'portal/add_driver',
            type: "POST",
            //dataType: "json",
            data:{user_email: user_email},
            success: function(data)
            {     
                $("#add_driver_modal").modal("hide");
                location.reload();
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Email already registered');
                location.reload();
            }
    });
};

<div class="modal fade" id="add_driver_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
          <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">ADD DRIVER</h4>
              </div>
              <div class="modal-body" id='add'>
                <div class="row" id="add_driver_form">
                
                    <input type="hidden" name="user_id" id="user_id" />

                    <div class="col-sm-4">
                    <input type="text" class="form-control input-lg" autocomplete="off" id="user_email"  name="user_email"  placeholder="Email" required>
                    </div>
                    
                </div>
              </div>
              <div class="modal-footer">
                <button type="submit" class="btn btn-default" data-dismiss="modal">Close</button>
                <button id="add" type="button" class="btn btn-primary btn-md" onclick=add_driver()>Add</button>
              </div>
            </div>
            <div id="form_submit_result"></div>
          </div>
        </div>
&#13;
&#13;
&#13;

我的控制器:

public function add_driver()
{
    $user_email = $this->input->post('user_email');

    $array = array(
        'user_email' => $user_email,
        'email_verified' => 1,
        'phone_verified' => 1
        );

    $this->db->select('*');
    $this->db->where($array);
    $this->db->from('user');
    $query = $this->db->get();
    $result = $query->row_array();
    $match_id = $result['user_id'];
    $match_email = $result['user_email'];

    if(!is_null($result))
    {
        $data_user_driver = array(
            'user_id' => $match_id,
            'user_email' => $match_email
            );
        $this->db->insert('user_driver',$data_user_driver);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在modal-body内的模式中添加.... <div class="modal-body" id='add'> <p style="display:none;" id="error" class="alert alert-danger"></p> ... 标记

else

并添加... if(!is_null($result)){ $data_user_driver = array( 'user_id' => $match_id, 'user_email' => $match_email ); $this->db->insert('user_driver',$data_user_driver); } else {//if email not verified echo 'err'; } .... 块并发送任何消息,如果没有找到结果(在add_driver方法中),如

success

现在在您的ajax .... success: function(data){ if(data == 'err'){ $('#error').show().text('Please verify your email'); return false; } $('#error').hide(); $("#add_driver_modal").modal("hide"); location.reload(); }, ....

中查看此结果
continue