1

我对使用 Jquery 相当陌生,并且正在为使用 CodeIgniter 和引导程序创建的简单站点创建登录名。提交登录按钮后,它不会显示任何错误或成功消息,这意味着我什至不知道它是否真的将数据发布到控制器这是我的代码,

jQuery代码

     <script>
  //Wait until the DOM is fully loaded
  $(document).ready(function(){
     //Listen for the form submit
     $('#loginform').submit(logIn);
   });

//The function that handles the process
function logIn(event)
{
   //Stop the form from submitting
   event.preventDefault();
        //Hide our form
               // $('#loginform').slideUp();

   //Collect our form data.
   var form_data = {
    email : $("[name='email']").val(),
    password : $("[name='password']").val(),

   };

  //Begin the ajax call
  $.ajax({
        url: "admin",
        type: "POST",     
        data: form_data,
        dataType: "json",
        cache: false,

        success: function (json) {             
            if (json.error==1)
            {
                //Show the user the errors.
               $('#message').html(json.message);
            } else {
                //Hide our form
                $('#loginform').slideUp();
                //Show the success message
                $('#message').html(json.message).show();
            }             
        }
      });
   }
</script>

登录.php

           <?php 
            echo $this->session->flashdata('alert');
        ?>
        <div id="message"></div>
<?php
    $attr = array('class' => 'admin-login form-horizontal well form-signin', 'id' => 'loginform');
    echo validation_errors('<div class="alert alert-error">', '</div>');
?>
<?php echo form_open(site_url('admin'), $attr) ?>
<!--<form action="<?php echo site_url('track-order'); ?>" method="post" class="form-horizontal form-search" id="trackModalform">-->
    <div class="control-group">
        <label class="control-label">Track Your Order</label>
    </div>
    <div class="control-group">
        <label class="control-label" >Email:</label>
        <div class="controls">
            <div class="input-prepend">
                <span class="add-on"><i class="icon-qrcode"></i></span>
               <input type="text" name="email" class="input-block-level email" placeholder="Email address">
            </div>
        </div>
    </div>
    <div class="control-group">    
        <label class="control-label" >Password:</label>
        <div class="controls">
            <div class="input-prepend">
                <span class="add-on"><i class="icon-key"></i></span>
               <input type="password" name="password" class="input-block-level password" placeholder="Password">
            </div>
        </div>
    </div>
    <div class="form-actions" style="margin-bottom: 0px; padding-bottom: 0px;">
        <input type="submit" class="btn  btn-primary " name="signin" value="Sign In!" id="login">
    </div>
</form>

我的控制器

    public function index()

{
        if (!file_exists('application/views/admin/index.php'))
        {
            //sorry that page is not available
            show_404();
        }                

                $this->form_validation->set_rules('email', 'Name', 'required|min_length[5]|max_length[50]|valid_email');
                $this->form_validation->set_rules('password', 'Password', 'required|min_length[5]');


        if($this->form_validation->run() === TRUE)
        {
            echo json_encode(array('error' => '1', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
        } else {
            //Save the data to the database, of course you will need all the data first.
           if($this->admin_model->validate_admin_login()):
             //Send the success to our javascript file.
             echo json_encode(array('error' => '0', 'message' => '<div class="alert alert-success"><strong>Success!</strong> You have been registered!</div>'));

           endif;     
        }

        $data['title'] =    ucfirst('Admin - Home');
        $data['currentpage'] =    'home';

        $this->load->view('admin/index', $data);
}

模型

public function validate_admin_login()
{
    $this->str = do_hash($this->input->post('password')); // SHA1 
    $this->db->where('email', $this->input->post('email'));
    $this->db->where('password', $this->str);
    $query = $this->db->get('ip_admin');

    if($query->num_rows == 1)
    {
         $data['admin_sess'] = $this->admin_model->admin_details($this->input->post('email'));
            $data = array(
            'email' => $this->input->post('email'),
            'is_admin_logged_in' => true
             );     
             $this->session->set_userdata($data);      
        return true;
    }
}

public function admin_details($user)
{         
    $query = $this->db->select('*')->from('ip_admin')->where('email', $user);
    $query = $query->get();
    return $data['admin_sess'] = $query->row();       
}

我并没有真正响应或输出任何消息来指示成功或失败,也许我一开始就搞错了。

我需要它来查询数据库,使用控制器上的 json 参数在视图页面上为我返回消息。

谢谢大家。

4

2 回答 2

2

我建议你像这样在 var_data 中添加一个数据:

    var form_data = {
         email : $("[name='email']").val(),
         password : $("[name='password']").val(),
         //add a data which is
         ajax: '1'
       };

并在您的控制器中检查它是否已发布:

    if($this->input->post('ajax'){
       //do something
     }else{
       //do something
    }

所以从那里你可以检查它是否工作。并在 Firefox 中安装 firebug 以进行调试。在 Chrome 中尝试检查元素并查看控制台

于 2013-08-02T02:31:08.847 回答
0

老实说,我还没有浏览过你所有的代码,因为它真的没有那么复杂,相反,如果你还没有安装它,我建议你安装Firebug来调试你的 jquery。它在使用 javascript 开发时必不可少。当事件被调用和处理时,它将打印任何错误或成功。

如何使用:Firebug 常见问题

编辑:

正如你要求的代码:

if($this->form_validation->run() === TRUE)
{
    echo json_encode(array('error' => '1', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
} else {
    //Save the data to the database, of course you will need all the data first.
   if($this->admin_model->validate_admin_login()):
     //Send the success to our javascript file.
     echo json_encode(array('error' => '0', 'message' => '<div class="alert alert-success"><strong>Success!</strong> You have been registered!</div>'));

   endif;     
}

$data['title'] =    ucfirst('Admin - Home');
$data['currentpage'] =    'home';

$this->load->view('admin/index', $data);

在此块中,您将回显 json 一次,然后吐出 HTML 视图。只需尝试删除:

$data['title'] =    ucfirst('Admin - Home');
$data['currentpage'] =    'home';

$this->load->view('admin/index', $data);

或者为您的请求创建单独的控制器函数,当您尝试将所有内容都塞入一个函数时,事情会变得非常混乱。

于 2013-08-02T00:07:14.967 回答