0
Test scenario
I have a page from login.php
, that checks the user and password with ajax, sending the data via post to the file valida.php
.
The valida.php
, when the authentication works, should redirect to another page, taking the data in $_POST
. But if you use a header
for example, it returns error
instead of success
.
Archives
login.php
<!DOCTYPE html>
<html>
<head>
<?php require_once 'html\head.php' ?>
<script type="text/javascript">
function login(){
var myData = $("#login").serialize();
arquivo = "valida.php";
$.ajax({
type: 'post',
data: myData,
url: arquivo,
success:function(response){
//alert(response);
$('#ret').html(response).show();
},
error:function(xhr, ajaxOptions, thrownError){
alert('error');
}
});
}
</script>
</head>
<body>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">
<img src="assets/img/truck.svg" width="30" height="30" class="d-inline-block align-top" alt="">
Teste
</a>
<form id="login" class="form-inline">
<input class="form-control mr-sm-2" type="text" name="user" placeholder="Usuário" aria-label="User">
<input class="form-control mr-sm-2" type="password" name="pswd" placeholder="Senha" aria-label="Pass">
<button class="btn btn-outline-warning my-2 my-sm-0" type="button" onclick="login();">Entrar</button>
</form>
</nav>
<div class="container">
<div id="ret"></div>
</div>
</body>
</html>
valida.php
set_time_limit(10);
$user = filter_input(INPUT_POST,'user');
$pswd = filter_input(INPUT_POST,'pswd');
if (isset($user) && isset($pswd)) {
if ($user == 'rbz' && $pswd == 123) {
header('location: ok.php');
} else {
echo '<br><div class="alert alert-danger alert-dismissible fade show" role="alert">
<strong>Atenção!</strong> Usuário ou senha incorretos.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div><br>';
}
} else {
echo "preencha os 2 campos";
}
- How could you redirect the page, keeping the data in
$_POST
?
Why don’t you send by
$_SESSION[]
?– Bulfaitelo
There are 2 cases. I don’t have a Septssion, but could already start on
valida.php
and redirect (would still have the problem of redirecting (do on returnsuccess
!? would be ideal!? ), and has the option of not opening thevalida.php
, then keep the question doubt.– rbz
There is no way to redirect post, so what you want there is no way to do. Why you need this data to be forwarded as post?
– Woss
I set up your environment here to do some tests, I came across 2 things, keeping the answer 302, ajax works correctly but making the return 200 ajax not even get the error. or anything on the console.
– Bulfaitelo