2013-02-21 120 views
1

我有我的網站的註冊表單,它顯示了用戶名,密碼和密碼確認,但我提供了所有這些。這是控制器代碼:使用codeigniter註冊時遇到問題

{ 

    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean'); 
    $this->form_validation->set_rules('email','Email','trim|required|valid_email'); 
    $this->form_validation->set_rules('pass1','Password','trim|required|min_length[4]|max_length[32]'); 
    $this->form_validation->set_rules('pass2','Password Confirmation','trim|required|matches[pass1]'); 

    if ($this->form_validation->run() === FALSE) 
    { 
     $password = $this->input->post('pass1'); 

     echo json_encode(array('error' => '1', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>'))); 
    } 
    else 
    { 
     $username = $this->input->post('username'); 
     $password = $this->input->post('pass1'); 

     $email = $this->input->post('email'); 
     $date = date('Y/m/d H:i:s'); 

     $this->load->model('ui_model'); 
     $this->ui_model->register_user($username,$password,$email,$date); 

     echo json_encode(array('error' => '0', 'message' => '<div class="alert alert-success"><strong>Success!</strong> You have been registered!</div>')); 

    } 

,這是視圖代碼:

<div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
     <h3>Registration</h3> 
    </div> 
    <div class="modal-body"> 
     <div id="registerModalerror" style="display: none;"></div> 
     <?php 
      $attributes = array('class' => 'form-horizontal','id' => 'registerModalform'); 

      echo form_open('',$attributes); 
     ?> 
     <div class="control-group"> 
      <label class="control-label" for="Username">Username</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
        <span class="add-on"><i class="icon-user"></i></span> 
        <input type="text" id="inputUser" placeholder="Username" name="username" value ="" > 
       </div> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label" for="inputEmail">Email</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-envelope"></i></span> 
       <input type="text" id="inputEmail" placeholder="email" name="email" value = ""> 
       </div> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label" for="inputPassword">Password</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-lock"></i></span> 
       <input type="password" id="inputPassword" placeholder="Password" name="pass1" value = ""> 
       </div> 
      </div> 
     </div> 
     <div class="control-group"> 
      <label class="control-label" for="inputPassword2">Retype Password</label> 
      <div class="controls"> 
       <div class="input-prepend"> 
       <span class="add-on"><i class="icon-lock"></i></span> 
       <input type="password" id="inputPassword2" placeholder="Retype Password" name="pass2" value = ""> 
       </div> 
      </div> 
     </div> 

     <div class="control-group"> 

      <div class="controls"> 
       <button type="submit" class="btn">Register</button> 
      </div> 
     </div> 
    </div> 
    <?php echo form_close(); ?> 
    <div class="modal-footer"> 


    </div> 

</div> 

<script> 
    $(document).ready(function() { 
      $('#myModalReg').removeData('modal'); 
      $('#myModalReg').hide(); 
      $('#registerModalform').submit(registerUser); 


     }); 

     function registerUser(event) 
     { 
      //Stop the form from submitting 
      event.preventDefault(); 

      //Collect our form data. 
      var form_data = { 
       username : $("[name='username']").val(), 
       password1 : $("[name='pass1']").val(), 
       password2 : $("[name='pass2']").val(), 
       email : $("[name='email']").val() 
      }; 

      //Begin the ajax call 
      $.ajax({ 
        url: "http://localhost/fys/index.php/ui/do_register", 
        type: "POST",  
        data: form_data, 
        dataType: "json", 
        cache: false, 
        async : false, 

        success: function (json) { 
         // alert(json.pass);  
         if (json.error==1) 
         { 
          $('#registerModalerror').html(json.message).show(); 
         } 
         else { 
          //Hide our form 
          $('#registerModalform').slideUp(); 
          //Show the success message 
          $('#registerModalerror').html(json.message).show(); 
         }    
        }, 

        error: function(json) 
        { 
         alert(json.message); 
        } 
      }); 
     } 

</script> 

我不知道哪裏出了問題,它是竊聽我幾乎24小時。任何人都可以提供幫助嗎?

謝謝!

回答

1

您的文章目的在於

{ 
    username : $("[name='username']").val(), 
    password1 : $("[name='pass1']").val(), 
    password2 : $("[name='pass2']").val(), 
    email : $("[name='email']").val() 
} 

但是你的控制器的POST名字是pass1pass2。你傳遞了password1password2

:P

+1

我不是最笨的人曾經走過這個星球上。 :( – 2013-02-21 08:35:27

+0

不要忘記標記答案爲接受,那麼你將不會:) – Atticus 2013-02-21 08:41:58