Codeigniter twitter引导程序使用Jquery登录


Codeigniter twitter bootstrap login using Jquery

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

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>

login.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参数为我返回消息。

谢谢大家。

我建议您在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中,尝试检查元素并查看控制台

老实说,我还没有看过你所有的代码,因为它真的没有那么复杂,相反,如果你还没有安装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);

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