否则,如果没有在ajax登录表单中工作

时间:2017-01-20 18:59:32

标签: javascript php jquery ajax

我通过查看google并结合proccess.php,提交表单和ajax来制作下面的代码,但我不知道我的错误在哪里。代码似乎不起作用。

proccess.php

<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

  //check login status and etc

      if ($data['status'] == 'ok' && $data['loggedinuser']['username'] == "$username") {  
          echo 'ok';
      } elseif ($data['message'] == 'checkpoint' && $data['status'] == 'fail') {
          echo 'checkpoint';
      } elseif ($data['errortype'] == 'invaliduser' && $data['status'] == 'fail') {
          echo 'wrongusername';
      } elseif ($data['errortype'] == 'badpassword' && $data['status'] == 'fail') {
          echo 'wrongpassword';
      } elseif ($data['errortype'] == 'unusablepassword' && $data['status'] == 'fail') {
          echo 'unusablepassword';
} else {
          echo '';
}
?>

的index.php

&#13;
&#13;
$(document).ready(function() {
  $("#loginform").submit(function() {
    if ($('#username').val().length > 3 || $('#password').val.length > 3) {
      var pdata = $(this).serialize();
      $.ajax({
        url: "proccess.php",
        data: pdata,
        type: "POST",
        beforeSend: function() {
          $("#pesan").html("<h4>Loading...</h4>");
        },
        success: function(pesan) {
          console.log(pesan);
          if (console.log(pesan) == "ok")
            $("#pesan").html("<h4>Login Success...<meta http-equiv='refresh' content='1'; url=redirect.php'></h4>");
          else if (console.log(pesan) == "gagal")
            $("#pesan").html("<h4>Error!</h4>");
          else if (console.log(pesan) == "wrongusername")
            $("#pesan").html("<h4>Wrong Username!</h4>");
          else if (console.log(pesan) == "wrongpassword")
            $("#pesan").html("<h4>Wrong Password!</h4>");
          else if (console.log(pesan) == "checkpoint")
            $("#pesan").html("<h4>need checkpoint</h4>");
          else if (console.log(pesan) == "accountblocked")
            $("#pesan").html("<h4>your account blocked.</h4>");
          else
            $("#pesan").html("<h4>Invalid username and/or password.</h4>");
        }

      });
    }
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header id="pesan" class="text-align-center">
  <h4>Login to your account</h4>
</header>
<form class="no-margin" method="POST" id="loginform">
  <fieldset>
    <div class="form-group">
      <label for="username">Username</label>
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-user"></i></span>
        <input name="username" id="username" type="username" class="form-control input-lg input-transparent" placeholder="Username" required>
      </div>
    </div>
    <div class="form-group">
      <label for="password">Password</label>
      <div class="input-group input-group-lg">
        <span class="input-group-addon"><i class="fa fa-lock"></i></span>
        <input name="password" id="password" type="password" class="form-control input-lg input-transparent" placeholder="Password" required>
      </div>
    </div>
  </fieldset>
  <div class="">
    <button id="masuk" type="submit" class="btn btn-block btn-lg btn-danger">
      <span class="small-circle"><i class="fa fa-caret-right"></i></span>
      <small>Sign In</small>
    </button>
  </div>
</form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

PHP中需要修复:
请收到Javascript发送的数据:

$data = $_REQUEST['data'];


Javascript中需要修复:
更改if语句如下:

if (pesan == "ok"){
    $("#pesan").html("<h4>Login Success...");
    window.location = "redirect.php";
}
else if (pesan == "gagal") 
    $("#pesan").html("<h4>Error!</h4>");
else if (pesan == "wrongusername") 
    $("#pesan").html("<h4>Wrong Username!</h4>");
else if (pesan == "wrongpassword") 
    $("#pesan").html("<h4>Wrong Password!</h4>");
else if (pesan == "checkpoint") 
    $("#pesan").html("<h4>need checkpoint</h4>");
else if (pesan == "accountblocked") 
    $("#pesan").html("<h4>your account blocked.</h4>"); 
else 
    $("#pesan").html("<h4>Invalid username and/or password.</h4>");


请注意如何使用Javascript完成重定向:

window.location = "redirect.php";

答案 1 :(得分:1)

你必须检查$ data数组中是否存在key,然后才能在else中运行它。您必须使用isset()来检查数组元素是否存在。

尝试按以下方式重新编写代码:

if($data['status']=="ok" && $data['loggedinuser']['username'] == "$username")
{
    echo "ok";
}
else
{
    if( isset($data['message']) ) {
      echo $data['message'];
    } 
    elseif( isset($data['errortype']) ) {
      echo $data['errortype'];
    }   
}

请注意,您可以直接回复$ data [“message”]和$ data [“error”]。