2016-09-02 11 views
1

私は検証のあるJQuery ajax登録フォームを持っています。すべての検証とフォームの提出はうまくいきます。しかし、フォーム提出後、成功したメッセージは機能しません。常にエラーメッセージがスローされます。私はcodeigniterを使用しています。Ajaxフォームは常にエラーメッセージを送信します

私のjqueryのは、以下の通りである

<script> 
    $(document).ready(function() { 

     (function($) { 
      "use strict"; 

      jQuery.validator.addMethod('answercheck', function(value, element) { 
       return this.optional(element) || /^\bcat\b$/.test(value); 
      }, "type the correct answer -_-"); 

      // validate contactForm form 
      $(function() { 
       $('#form_register').validate({ 
        rules: { 
         email: { 
          required: true, 
          email: true 
         }, 
         username: { 
          required: true, 
          minlength: 6 
         }, 
         full_name: { 
          required: true, 
          minlength: 3 
         }, 
         password: { 
          required: true, 
          minlength: 6 
         }, 
         confirm_pass: { 
          required: true, 
          minlength: 6, 
          equalTo: "#password" 
         }, 
         phone: { 
          required: true, 
          number: true, 
          minlength: 10 
         } 
        }, 
        messages: { 
         email: { 
          required: "no email, no account" 
         }, 
         username: { 
          required: "come on, you have to provide username", 
          minlength: "your username must consist of at least 6 characters" 
         }, 
         full_name: { 
          required: "come on, you have a name don't you?", 
          minlength: "your name must consist of at least 3 characters" 
         }, 
         password: { 
          required: "Please provide password!", 
          minlength: "thats all? really?" 
         }, 
         confirm_pass: { 
          required: "Please confirm password!", 
          minlength: "thats all? really?", 
          equalTo: "Please enter the same password again." 
         }, 
         phone: { 
          required: "come on, you have a phone don't you?", 
          number: "Phone number must contain digits only", 
          minlength: "your phone number must consist of at least 10 characters" 
         } 
        }, 
        submitHandler: function(form) { 
         $('#loading').show(); 

         $(form).ajaxSubmit({ 
          type: "POST", 
          data: $(form).serialize(), 
          url: "<?php echo base_url(); ?>createcode", 
          success: function(data) { 
           $('#loading').hide(); 

           $('#success').show(); 
           $('#success').html(data); 

           if (data.indexOf('login now!') > 0) { 
            $(form).trigger("reset"); 
           } 
          }, 
          error: function(req, status, err) { 
           console.log('something went wrong', status, err); 
           alert('something went wrong' + status + err); 
          } 
         }); 
        } 
       }); 
      }); 

     })(jQuery) 
    }); 
</script> 

私のコントローラ(Createcode.phpは)

class Createcode extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->model('creates'); 
    } 

    public function index() { 
     if($this->input->post('register')) { 
      $email = $this->input->post('email'); 
      $username = $this->input->post('username'); 
      $full_name = $this->input->post('full_name'); 
      $confirm_pass = $this->input->post('confirm_pass'); 
      $phone = $this->input->post('phone'); 

      $add = $this->creates->createAccount($email, $username, $full_name, $confirm_pass, $phone); 

      if($add === TRUE) { 
       echo "Your account created. You can login now!"; 
      } else if($add === FALSE) { 
       echo 'Something went wrong. Tryagain later!'; 
      } else if($add == 'email') { 
       echo 'Email already exists. Try another!'; 
      } else if($add == 'username') { 
       echo 'Username already exists. Try another!'; 
      } else if($add == 'phone') { 
       echo 'Mobile number already exists. Try another!'; 
      } else { 
       echo 'Something went wrong. Tryagain later!'; 
      } 
     } 
    } 
} 

私のモデル(creates.php)以下の通りである

class Creates extends CI_Model { 

    public function createAccount($email, $username, $full_name, $confirm_pass, $phone) { 
     $check_username = $this->db->query('SELECT * FROM tbl_account WHERE acc_username = "'.$username.'"'); 
     $check_phone = $this->db->query('SELECT * FROM tbl_account WHERE acc_phone = "'.$phone.'"'); 
     $check_email = $this->db->query('SELECT * FROM tbl_account WHERE acc_email = "'.$email.'"'); 

     if($check_email->num_rows() > 0) { 
      return 'email'; 
     } else if($check_username->num_rows() > 0) { 
      return 'username'; 
     } else if($check_phone->num_rows() > 0) { 
      return 'phone'; 
     } else { 
      $data = array(
       'acc_email' => $email, 
       'acc_username' => $username, 
       'acc_name' => $full_name, 
       'acc_password' => $this->encrypt->encode($confirm_pass), 
       'acc_phone' => $phone, 
       'acc_created_on' => date("l, d/F/Y h:i:s A") 
      ); 

      $add = $this->db->insert('tbl_account', $data); 

      if($add == 1) { 
       return TRUE; 
      } 
      else { 
       return FALSE; 
      } 
     } 
    } 
} 

を下回っています上記のコードは正常に動作しています。そして、データベース操作も機能しています。
成功フォーム送信後something went wrongメッセージerror:機能が表示されています。
このエラーを解決する方法。私はたくさん試しました。解決策はありますか?

+0

javascriptコンソールまたはサーバーのログを確認して、正確な応答/問題が – jeroen

+0

@jeroenであることを確認する必要があります。 –

+0

Ctrlキーを押しながらK(firefox)を押すか、firebugエクステンションをインストールする –

答えて

0

私は主な問題は、この点を考慮してください...私はあなたがセレクタ

$(`form`).ajaxSubmit({... 

なく、あなたの問題に関連して、いくつかの引用符が必要だと思うが、私は自分自身を助けることができない、ここで

$(form).ajaxSubmit({... 

である疑いがありますあなたのモデル関数のリファクタ

public function createAccount($email, $username, $full_name, $confirm_pass, $phone) 
{ 
//No point in making further queries if any given validation fails. 
//So check the results after each query and return if it fails 
    $check_username = $this->db->query('SELECT * FROM tbl_account WHERE acc_username = "'.$username.'"'); 
    if($check_email->num_rows() > 0) { 
     return 'email'; 
    } 

    $check_phone = $this->db->query('SELECT * FROM tbl_account WHERE acc_phone = "'.$phone.'"'); 
    if($check_username->num_rows() > 0) { 
     return 'username'; 
    } 

    $check_email = $this->db->query('SELECT * FROM tbl_account WHERE acc_email = "'.$email.'"'); 
    if($check_phone->num_rows() > 0) { 
     return 'phone'; 
    } 

    $data = array(
     'acc_email' => $email, 
     'acc_username' => $username, 
     'acc_name' => $full_name, 
     'acc_password' => $this->encrypt->encode($confirm_pass), 
     'acc_phone' => $phone, 
     'acc_created_on' => date("l, d/F/Y h:i:s A") 
    ); 
    //don't need all this 
    //$add = $this->db->insert('tbl_account', $data); 
     //if($add == 1) { 
      //return TRUE; 
     //} 
     //else { 
      //return FALSE; 
     //} 

    //when this does exactly the same thing 
    return $this->db->insert('tbl_account', $data); 
} 

この条件セットをCreatecode::index

if($add === TRUE){ 
    echo "Your account created. You can login now!"; 
} else if($add == 'email'){ 
    echo 'Email already exists. Try another!'; 
} else if($add == 'username'){ 
    echo 'Username already exists. Try another!'; 
} else if($add == 'phone'){ 
    echo 'Mobile number already exists. Try another!'; 
} else { 
    echo 'Something went wrong. Tryagain later!'; 
} 

それは一つの条件の評価を排除して、あなたが持っているか、多分速く1と同じように高速に実行されます。

関連する問題