Redirect with PHP After Ajax Call

Redirect with PHP after ajax call

You redirect in success:

$('#save_sale').click(function() {
var save_sale = 1;
$.ajax({
type: 'GET',
url: 'summary.php',
data: {save_sale: save_sale},
success: function(data) {
window.location.href = 'addcust.php?new_sale=' + data
},
error: function(xhr, ajaxOptions, thrownerror) { }
});
});

Whatever you echo back from the PHP script will be in data. So echo $sale_id and you'll have your URL.

redirect page upon form submit after AJAX request that calls PHP function

If you want to redirect to another page, put this after form is successfully submited

window.location = "http://www.example.com/thankyou.html";

EDIT: You shouldn't handle this redirect in PHP, because this is asynchronous call. You need to reference this somehow in you submit script

Hope it helps

PHP redirect from Ajax Call

In lieu of an answer that actually works, I've figured out a workable solution. I'm not sure whether this is the correct way of doing this but it works well.

In my validator.php when the form values are correct I put the following:

if ( // form data is valid ) {

echo 'redirect';

}

Then, on my login page, when returning the string from the php page I put this:

function updatepage(str){

if (str.match(/redirect/)) {
window.location.replace('welcome.php');
}
else {
document.getElementById("result").innerHTML = str;
}
}

The idea is that when validator.php confirms the login credentials are correct it returns a string.

If that string matches "redirect", JavaScript will redirect the page.

If anyone has any input on this, please comment. I feel this is a pretty good solution. Surprised I didn't think of it earlier.

Execute PHP redirect after ajax request

Set the status to 302 too so that the browser does the redirect.

$response = $response->withStatus(302);
return $response->withHeader('Location', $connection->login_url( 'someRandomString' ) );

Alternatively, as you're using Slim, you can use withRedirect:

return $response->withRedirect($connection->login_url('someRandomString'));

Redirect with PHP on Ajax call

In your function.php perform your actions and then add below lines at the end where you send response

$login_status = false;
if (defined('RESTRICTED')) {
if (!isset($_SESSION['logged'])) {
$login_status = true;
}
}

$response = array();
$response['login_status'] = $login_status;

echo json_encode($response);

In your JS code:

$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: noteSequence,
success: function(data) {
data = JSON.parse(data);
if(data['login_status']) {
location.replace("index.php")
} else {
//do something else
}
}
});

Redirect to another page in PHP through AJAX

Use proper AJAX format to handle the response in client side here is the modified code

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

      <?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);
?>


Related Topics



Leave a reply



Submit