2017-02-03 7 views
0

AJAX、PHP、およびMySqlを使用してログインしようとしていますが、AJAXを使用してPHPクエリと通信できません。なぜ私はajaxを使用してクエリを使用してデータベースに接続できませんか?

これは私のコードされています。最近では、Google Chromeが更新されました

これは私のPHPのクエリ

<?php 
include_once("../Connector/db_open.php"); 
if(isset($_POST['btnLogin'])) { 
    $user_name = trim($_POST['user_email']); 
    $pass_word = trim(md5($_POST['password'])); 

     if (empty($user_name) && empty($pass_word)) 
     { 
      echo "Please fill-up everything!!!"; 
     } 
     else 
     { 
      $login = "SELECT uid, user_name, pass_word, email FROM tbluser WHERE user_name = '$user_name'"; 
      $result = mysqli_query($conn, $login) or die("database error:". mysqli_error($conn)); 

      if ($result->num_rows >0) 
      { 
       $row = mysqli_fetch_assoc($result);  
       session_start(); 
       $_SESSION["uid"] = $row["uid"]; 
       $_SESSION["user_name"] = $row["user_name"]; 
       $user_id = $row["uid"]; 
       $user_status = $row["status"]; 

       if ($user_name != $row["user_name"]) 
       { 
        echo "User not exist"; 
       } 
       else 
       { 
        echo "Successfully Login"; 
        $_SESSION['user_id'] = $user_id; 
        header("location: ../Pages/index.php"); 
       } 
      } 
     }  
    } 
?> 

である。これは私のAJAXとHTML

<?php 
    session_start(); 
    if (isset($_SESSION["user_id"])) 
    { 
     header("location: Pages/index.php"); 
    } 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
<!-- jQuery --> 
<link rel="apple-icon-precomposed" sizes="144x144" href="assets/ico/apple-icon-144-precomposed.png"> 
<link rel="apple-icon-precomposed" sizes="114x114" href="assets/ico/apple-icon-114-precomposed.png"> 
<link rel="apple-icon-precomposed" sizes="72x72" href="assets/ico/apple-icon-72-precomposed.png"> 
<link rel="apple-icon-precomposed" href="assets/ico/apple-icon-57-precomposed.png"> 
<link rel="shortcut icon" href="assets/ico/favicon.ico"> 
<?php 
include('include/header.php'); 
include_once("Connector/db_open.php"); 
?> 
<title>RAFI</title> 

<script type="text/javascript"> 
    $(document).ready(function(){ 

     $("#btnLogin").click(function(event){ 
      event.preventDefault(); 
      $.ajax({ 
       url: "query/login_query.php", 
       method: "POST", 
       data: $('form').serialize(), 
       dataType: "text", 
       success: function(response) { 
        $("#btnLogin").html('<img src="images/ajax-loader.gif" /> &nbsp; Connecting ...'); 
        if (response=="Please fill-up everything!!!") 
        { 
         alert("Please fill-up everything"); 
        } 
        else if(response=="User not exist") 
        { 
         alert("User not exist"); 
        } 
        else if(response=="Successfully Login") 
        { 
         setTimeout(' window.location.href = "Pages/index.php"; ',4000); 
        } 
       } 
      }); 
     }); 

    }); 
</script> 

<?php include('include/navbar.php');?> 
<div class="container"> 
     <div class="row text-center " style="padding-top:10px;"> 
      <div class="col-md-12"> 
       <img src="images/logo.png" style="width: 180px;" /> 
      </div> 
     </div><br /> 
     <div class="row"> 
      <div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-6 col-xs-10 col-xs-offset-1"> 
       <form method="post"> 
        <h4 class="form-login-heading">User Log In</h4><hr /> 
        <div class="form-group input-group"> 
         <span class="input-group-addon"><i class="glyphicon glyphicon-tag" ></i></span> 
         <input class="form-control" type="text" id="user_name" name="user_name" placeholder="Username" required /> 
        </div> 
        <div class="form-group input-group"> 
         <span class="input-group-addon"><i class="glyphicon glyphicon-lock" ></i></span> 
         <input class="form-control" type="password" id="pass_word" name="pass_word" placeholder="Password" required/> 
        </div> 
        <hr /> 
        <div class="form-group"> 
         <button type="submit" class="btn btn-default" name="btnLogin" id="btnLogin"> 
         <span class="glyphicon glyphicon-log-in"></span> &nbsp; Sign In 
         </button> 
        </div> 
       </form> 
      </div> 
     </div>   
</div> 
<?php include('include/footer.php');?> 
<div class="insert-post-ads-1" style="margin-top:20px;"> 
</div> 
</div> 
</body></html> 
+0

あなたのコンソールログからすべてのエラー? –

+0

エラーはありませんが、まだ動作していません。 – Newbie

+0

あなたはどのブラウザを使用していますか?そしてバージョン? F12を押してコンソールログを表示します。次に、フォームを提出し、そこにエラーがあるかどうかを確認します。 –

答えて

0

です。このアップデートでは、(IE、Edge、その他のブラウザのようなもの)を送信したときには<input type="button">または<button type="submit"></button>は表示されません。あなたは何ができるか

は、あなたのシリアライズされたデータに手動でボタン名を追加することです:

var formData = $('form').serialize(); 
    formData.append('btnLogin', true); 

そして、あなたのAJAXの:

.... 
    url: "query/login_query.php", /* DIRECTORY AND FILE WHERE YOU WILL SUBMIT THE DATA */ 
    type: "POST", /* TRY USING TYPE INSTEAD OF METHOD */ 
    data: formData, /* DATA TO PASS ON */ 
.... 
関連する問題