2017-04-08 37 views
-1

當我通常使用action =「」的方式將我的表單提交給PHP時。但是,當我提交使用AJAX時,它一直警告「ILD」...無效的日誌詳細信息。 請問我做錯了什麼?Ajax接收錯誤的數據,但PHP工作正常

**這是我的形式**

<form action="" method="post" onsubmit="return do_signIn()"> 
         Username: 
         <input id="#user" type="text" class="form-control" placeholder="Username" name="username" /> 
         Password: 
         <input id="#pass" type="password" class="form-control" placeholder="Password" name="password" /> 
         <br /> 
         <input id="login" type="submit" class="btn btn-danger" value="Login" name="login"/> <span><a href="#"><i class="fa fa-lock"></i> Forget Password?</a></span> 
         <br /><br /> 
         <p id="error-area"></p> 
        </form> 

**下面是我的AJAX JQUERY **

<script type="text/javascript"> 
     function do_signIn() { 
      var username = $("#user").val(); 
      var password = $("#pass").val(); 
      var login = $("#login").val(); 
      if(username!="" && password!=""){ 
       $("#error-area").html("<i class='fa fa-spinner fa-spin'></i> Loading..."); 
       $.ajax 
      ({ 
      type:'post', 
      url:'b-sign-in.php', 
      data:{ 
        do_signIn:"b-sign-in", 
        username:username, 
        password:password, 
        login:login, 
      }, 
        success:function(response) { 
      if(response=="VA"){ 
        alert("Verify Your Account" + response); 
        //window.location.href=""; 
      }else if(response=="SUCCESS"){ 
        alert("Success! You are In"); 
        //window.location.href=""; 
      }else if(response=="ELI"){ 
        alert("Error Loggin In"); 
        //window.location.href=""; 
      }else if(response=="ILD"){ 
        alert(response); 
        //window.location.href=""; 
      }else{ 
      $("#error-area").html("Something Went Wrong"); 
      alert(response); 
      //$("#error-area").slideUp(5000); 
      } 
      } 
      }); 
      }else{ 
      $("#error-area").html("Please fill all fileds!"); 
      //$("#error-area").slideUp(5000); 
      }  
      return false; 
      } 
</script> 

和**這是我的PHP **

if(isset($_POST['login']) && $_POST['login']=="Login"){  
     //check user input 
     $username = sanitizeMySQL($con, $_POST['username']); 
     $password = sanitizeMySQL($con, $_POST['password']); 

     //harsh the password 
     //Harsh the password 
     $harshQuery = "SELECT info_value FROM b_info WHERE info_name = 'siteHarsh'"; 
     $harshRun = mysqli_query($con, $harshQuery); 
     $harshRow = mysqli_num_rows($harshRun); 
     if(!$harshRow || $harshRow > 1 || $harshRow < 1){ 
      echo "error"; // Show An unexpected error occured 
      exit(); 
     }else{ 
      $passHarsh = mysqli_fetch_assoc($harshRun); 
      $harsh = $passHarsh['info_value']; 
      $correctPassword = md5($harsh . $password . $harsh); 
     } 

     $run = " 
        SELECT * FROM b_user 
        WHERE user_name = '$username' 
        AND user_password = '$correctPassword' 
       "; 
     $result = mysqli_query($con, $run); 
     $rowno = mysqli_num_rows($result); 

     //Check if detail match in db 
     if($rowno == 1){ 
      $row = mysqli_fetch_array($result); 
      $_SESSION['loggeduser'] = TRUE; 
      $_SESSION['username'] = $row['user_name']; 
      $_SESSION['email'] = $row['user_email']; 

      if($row['user_activation'] == 0){ 
       echo "VA"; //Verify Your Account By Entering a Valid Phone Number 
       exit(); 
      }else if($row['user_activation'] == 1){ 
       echo "SUCCESS"; 
       exit(); 
      } 
      else{ 
       echo "ELI"; //Error Logging In 
       exit(); 
      }   
     } 
     else { 
      echo "ILD"; //Invalid Login Details 
      exit(); 
     } 
//When No Post of Signup or SignIn is discovered 
}else { 
    header("Location:". SITE_HOME); 
} 
+0

做一些基本的調試和登錄瀏覽器控制檯調試輸出。還要檢查瀏覽器開發工具網絡中的實際請求,看看發送的是預期的結果 – charlietfl

回答

0

之前,你不應該使用的表單元素是這樣的:

你必須寫ID = 「用戶」 爲ID = 「#用戶」。你不能這種類型的JavaScript獲得價值。 這個錯誤:

<input id="#user" type="text" class="form-control" placeholder="Username" name="username" /> 

這是正確的:

<input id="user" type="text" class="form-control" placeholder="Username" name="username" /> 
+0

謝謝你的觀察。這就是爲什麼與團隊一起工作非常好。我幾個小時都沒有看到。我甚至不相信我會犯這樣愚蠢的錯誤。非常感謝。你拯救了我的一天 –

3

您的html格式的input-id是#username和#password。刪除井號標籤,它會正常工作。

+0

你說的是親愛的@Morosko,你在哪裏?你在說什麼input-id?請列出哪些腳本? –

相關問題