How can I do PHP redirect header in Laravel 4?
In original PHP:
header('Location: http://www.example.com/');
I don't want to do with return Redirect::route('loginpage');. Because it does not work well with ajax. So, how can I do the PHP redirect with Laravel?
Thanks.
If you're wanting to get the url and refresh the page with an ajax function you could simply return the laravel route url and refresh the page with javascript:
// Use the route helper function
return route('loginpage');
// Javascript page redirect
var url = 'Your ajax response here';
window.location.href = url;
return Redirect::to('loginpage')
Is the most common way. Why doesn't that work well with ajax?
When using AJAX, your request isn't handled the way you're expecting it to be. This isn't a laravel-specific issue, as your header() location example would not work either.
If you want to make an ajax request that redirects the user, you'll need to have the response from ajax return something that you can key off of in javascript that executes a window.location change based on the results.
Related
After user's filled the input fields and has submitted I use an ajax request to complete the sign up process.In this register.php I'd like to redirect the user to another page after I inserted their info to the database. Everything is fine and nothing is outputted until header() function. The code below does not redirect and as if nothing happened the callback function of ajax request is processed. I am really stuck here, I don't know what to do.I can try javascript solutions but I'd prefer php solution over that one.
Code:
header("Location: http://localhost/profil.php", true, 307);
exit();
You can use JavaScript to redirect in the ajax success callback function:
success: function(data) {
window.location.href = 'http://localhost/profil.php';
}
It can't be done with a PHP header redirect, because that will only redirect the ajax call, not the original browser window.
I need to pass the URL of a page to a PHP POST function which will return a document. I was wondering if it is possible to pass in the URL via a function or will it have to be stored in a variable? Or is it even possible to do this? I have this function to retrieve the URL of the previous page:
$(document).ready(function() {
var referrer = document.referrer;
});
And this is the PHP POST method which does not work:
$_POST['http://<THE URL I AM SENDING THE REQUEST TO>&url=<THE URL OF THE PREVIOUS PAGE>'];
I had it set up like:
$_POST['http://<THE URL I AM SENDING THE REQUEST TO>&url=referrer'];
But it would not run. Does anyone know how I can get this to work? Thanks
What is the best way to handle Ajax vs. Full page requests with PHP ?
I mean if I click a button that calls Ajax function and loads necessary info into a div and changes the URL.
How do I make that link work if the user copy it and use later to reach the same page.
In some of the apps I separate it with a hash. Something like a router and my code goes something like this:
var preRoute = document.URL.split('#');
if(preRoute[1] != undefined) {
//ajax call
} else {
//default page
}
you can try Jquery Adress http://www.asual.com/jquery/address/,
demo here
http://www.asual.com/jquery/address/samples/state/
or
http://vietcode.vn
You could check the headers. jQuery for example will add the following key/value to the header: X-Requested-With: XMLHttpRequest for ajax calls. See also this question
Using: jquery pjax - https://github.com/defunkt/jquery-pjax
you can detect on server side if you get X-PJAX request header
And a great feature of this plugin is the history manipulation and ease of use.
i am trying to send header from a php file that is called with ajax. problem is, when i validate all information with php, i want to redirect to another page but headers dont work! any ideas?
what the code does:
//------------- index.php
on click of input, function passes <input> values through $_POST to login.php
if($_SESSION[superman]==true){redirects to index_main.php}
//------------- login.php
all it does is validate forms and if login and password are correct, it sets $_SESSION [superman]=true
The redirect will only redirect the Ajax call! If you want to change the displayed page in the browser, you'll need to redirect using Javascript once the Ajax call completes.
Make your Ajax call return a value indicating successful login (for example the string true or maybe the URL to the page you want to load)
Check the return value of the Ajax call. If success, change location.href using Javascript.
i have a jquery Ajax request happening on a page. On php side i am checking if the session is active and doing something. If the session is not active i want to redirect the user to another page in php(header redirect). how do i do it.
I know how to achieve it in javascript(i.e if session_fail then change window.location but is there something that i can do in php/cakephp
Redirects only say "The data you requested can be found here".
HTTP provides no way to say "Even though you requested a resource to go inside a page, you should should leave that page and go somewhere else".
You need to return a response that your JavaScript understands to mean "Go to a different location" and process it in your own code.
If I understand what you want to happen then this is how I'm implementing it. It's in Prototype instead of jQuery but it shouldn't take you long to translate:
new Ajax.Request('process.php', {
on401: function(response) {
var redirect = response.getHeader('Location');
document.location = redirect;
}
});
In your PHP, output the following if the session is inactive:
header('Location: http://example.com/login.php', true, 401);
exit;
This is what you would want in your php IF THIS WERE A REGULAR REQUEST, NOT AN AJAX
if (isset($_SESSION)) doSomething();
else header("Location: otherUrl");
Since this is an Ajax call, you are not passing control to the php, but just trying to get a response that (likely) fills a particular section of your page. You do not mention what jQuery ajax function you use, but it matters. I would imagine you are using either $.get() or $(element).load() ??
Without knowing the particulars, this is my best suggestion for you.
Ajax call: $.get(url, callbackFunc);
php:
if(isset($_SESSION)) echoSomething() else echo("redirect");
callbackFunc:
function(data)
{ if (data == "redirect") window.location = otherUrl;
else
$("#desiredElement").html(data);
}