通过AJAX重定向到PHP中的另一个页面 [英] Redirect to another page in PHP through AJAX
问题描述
我的页面正在从javascript收集信息并将其发送到PHP,然后再发送到MySQL,问题是我希望根据数据库中的数据将其重定向到其他页面,我尝试使用标头,但它只是在警报中向我显示了另一页的整个HTML代码,我不想要那样.我希望它根据情况重定向到一页或另一页
My page is gathering info from javascript and sending it to PHP and then to MySQL, issue is that i want it to redirect to different pages depending on the data i have in the DB, I've tried to use header but it just shows me the whole HTML code of the other page in the alert and I don't want that. I want it to redirect to one page or another depending on the condition
HTML(Login.html)
HTML (Login.html)
<div class="wrap-input100 validate-input" data-validate = "Enter username">
<input class="input100" type="text" id="user" name="username" placeholder="Email">
<span class="focus-input100" data-placeholder=""></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
<input class="input100" type="password" id="pass" name="pass" placeholder="Password">
<span class="focus-input100" data-placeholder=""></span>
</div>
<div class="container-login100-form-btn">
<a class="login100-form-btn" id = "logBtn">
Login
</a>
</div>
脚本
$('#logBtn').click(function(event){
user = document.getElementById("user").value;
password = document.getElementById("pass").value;
$.ajax({
type:"POST",
url:"login.php",
async: false,
data: {user:user,password:password},
success: function(data){
alert(data);
//window.location = '../Main/index.html';
}
});
});
PHP
<?php
$servername = "localhost";
$username = "root";
$password = "tbjdjkdl";
$dbname = "dbbbbbb";
$conn = new mysqli($servername, $username, $password, $dbname);
$user = $_POST['user'];
$pass = $_POST['password'];
$sql = "SELECT * FROM users WHERE email='$user' AND clave='$pass'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$sql = "SELECT * FROM users WHERE email='$user' AND clave='$pass' AND permisos='Administrador'";
if (mysqli_num_rows($result) > 0){
echo "admin";
header('Location: ../Main/index.html');
exit;
}
else{
echo "user";
header('Location: ../Main/startemp.html');
exit;
}
} else {
$msg = "username/password invalid";
echo $msg;
}
mysqli_close($conn);
?>
推荐答案
使用正确的AJAX格式在客户端处理响应,此处是修改后的代码
Use proper AJAX format to handle the response in client side here is the modified code
login.html
login.html
<div class="wrap-input100 validate-input" data-validate = "Enter username">
<input class="input100" type="text" id="user" name="username" placeholder="Email">
<span class="focus-input100" data-placeholder=""></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
<input class="input100" type="password" id="pass" name="pass" placeholder="Password">
<span class="focus-input100" data-placeholder=""></span>
</div>
<div class="container-login100-form-btn">
<a class="login100-form-btn" id = "logBtn">
Login
</a>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
$('#logBtn').click(function(event){
user = document.getElementById("user").value;
password = document.getElementById("pass").value;
$.ajax({
type:"POST",
url:"login.php",
async: false,
data: {user:user,password:password},
success: function(data){
alert(data);
if(data=="admin"){
window.location="https://..Main/index.html";
}
if(data=="user"){
window.location="https://....startemp.html";
}
}
});
});
</script>
login.php
login.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$conn = new mysqli($servername, $username, $password, $dbname);
$user = $_POST['user'];
$pass = $_POST['password'];
$sql = "SELECT * FROM users WHERE email='$user' AND clave='$pass'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$sql_1 = "SELECT * FROM users WHERE email='$user' AND clave='$pass' AND permisos='Administrador'";
$result_1 = mysqli_query($conn, $sql_1);
if (mysqli_num_rows($result_1) > 0){
echo "admin";
exit(0);
}
else{
echo "user";
exit(0);
}
} else {
$msg = "username/password invalid";
echo $msg;
}
mysqli_close($conn);
?>
这篇关于通过AJAX重定向到PHP中的另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!