Php- Ajax and Redirect

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.

How to manage a redirect request after a jQuery Ajax call

The solution that was eventually implemented was to use a wrapper for the callback function of the Ajax call and in this wrapper check for the existence of a specific element on the returned HTML chunk. If the element was found then the wrapper executed a redirection. If not, the wrapper forwarded the call to the actual callback function.

For example, our wrapper function was something like:

function cbWrapper(data, funct){
if($("#myForm", data).length > 0)
top.location.href="login.htm";//redirection
else
funct(data);
}

Then, when making the Ajax call we used something like:

$.post("myAjaxHandler", 
{
param1: foo,
param2: bar
},
function(data){
cbWrapper(data, myActualCB);
},
"html"
);

This worked for us because all Ajax calls always returned HTML inside a DIV element that we use to replace a piece of the page. Also, we only needed to redirect to the login page.

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

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.

How to use header redirect in PHP AJAX Call In Pure Javascript?

Make an ajax call.

<?php
header('Content-Type: application/json');
echo json_encode(['location'=>'/user/profile/']);
exit;
?>

The ajax response will return something like

{'location':'/user/profile/'}

In your ajax success javascript function

 xhr.onreadystatechange = function () {
if (xhr.status >= 200 && xhr.status <= 299)
{
var response = JSON.parse(xhr.responseText);
if(response.location){
window.location.href = response.location;
}
}

}

How to redirect with jquery ajax and php response

You are missing dataType property for ajax to decode the response:

jQuery.ajax({
type: "POST",
dataType: "json",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response) {
//console.log(response.redirect);
if (response.redirect) {
window.location.href = response.redirect_url;
} else {
// Process the expected results...
}
}
})


Related Topics



Leave a reply



Submit