weixin_33713503 2017-05-04 22:16 采纳率: 0%
浏览 30

如何在Ajax中使用标头

I'm creating a login checker using jQuery and Ajax, i'm calling query.php which verifies the username and password. And if they don't match, an alert will be shown, and if they are correct it will redirect the user to the other page. The error message works just fine. The problem is that when i enter correct username and password, the page doesn't redirect. In my case i'm been redirected twice, from login.php to query.php then to groupes.php or seance.php

query.php

<?php
require('queries/dbsetup.php');

$username = $_POST['username'];
$password = $_POST['password'];
$type = "";
if (empty($username) || empty($password)) { ?>
    <script>
        sweetAlert("Oops...", "Veuillez remplir tous les champs !", "error");
    </script>
<?php
} else {
echo $type;
switch ($_POST['type']) {
case 'etudiant' : $type = 'etudiant';break;
case 'enseignant' :$type = 'enseignant';break;
}

// Check the username and password
$query = "SELECT username FROM ".$type." where username = '".$username."' AND password ='".$password."' ;";
$set = mysqli_query($conn,$query);
$run = mysqli_fetch_array($set);
$id = $run['username'];

// Save the type
$_SESSION['type'] = $type;


if (!empty($id)) {
$_SESSION['user_id'] = $username;

switch ($type) {
case 'etudiant':
header('location: ../groupes.php',true);exit;break;
    case 'enseignant':
header('location: ../seance.php',true);exit;break;

}

} else { ?>
    <script>
        sweetAlert("Oops...", "Le nom d'utilisateur et le mot de passe ne sont pas identique !", "error");
    </script>
    <?php
}
}

?>

login.php

<form action="query.php" method="post" id="e1" class="">
              <h4>Nouvelle seance</h4>
              <input name="username" placeholder="Username" type="text" class="form-control">
              <input name="password" placeholder="Password" type="password" class="form-control">
              <input name="type" placeholder="Type" type="text" class="form-control"> 
              <button id="login">Login</button>                 
              <p id="result"></p>
</form>

login.js

$('#e1').submit(function(){
 return false;
});

$('#login').click(function(){
 $.post(
 $('#e1').attr('action'),
 $('#e1 :input').serializeArray(),
 function(result){
 $('#result').html(result);
 }
 );
});
  • 写回答

1条回答 默认 最新

  • weixin_33716557 2017-05-04 22:28
    关注

    You can achieve the desired behavior by telling the client to load the new page via javascript.

    You can change these lines

    switch ($type) {
        case 'etudiant':
            header('location: ../groupes.php',true);exit;break;
        case 'enseignant':
            header('location: ../seance.php',true);exit;break;
    }
    

    To

    switch ($type) {
        case 'etudiant':
            ?>
            <script>
                window.location = "../groupes.php";
            </script>
            <?php
            exit;
            break;
        case 'enseignant':
            ?>
            <script>
                window.location = "../seance.php";
            </script>
            <?php
            exit;
            break;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价