Ajax request - determine current wordpress page in functions.php - php

I've tried to determine the current page in functions.php with an if (is_home()) function, because I only want to run the specific ajax request on my homepage, but it always returns false and if I request the current URI, it says /wp-admin/admin-ajax.php/wp-admin/admin-ajax.php, the URI I actually request in my JS Ajax function.
So how can I only request my current page instead of the Ajax request?
Thanks for any help!

In the javascript
var pathname = window.location.pathname;
Then you can pass it with the rest of your data to the php function.

Related

Detemine directory path of originating URL for Ajax request [duplicate]

How can I get the url of the referer in an ajax request?
A file named main.php has jQuery that sends an ajax call to a file named request.php.
Is it possible to somehow figure out the referer when I'm on request.php? To be precise, I want to print the string "main.php" (dynamically) while running request.php.
Normally the the browser will send the referer page using the header Referer as part of the ajax request so you can read it
So you could do something like
$_SERVER['HTTP_REFERER']
How do you get the 'referer' header in PHP?
If you don't want to depend on the default Referer header, pass a custom header of your own
$(document).ajaxSend(function (event, jqXHR) {
jqXHR.setRequestHeader('my-referer', 'some-value');
});
yes on request.php u can get referee like this $_SERVER['HTTP_REFERER']

Passing the URL of a page to a PHP POST call

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

Laravel 4 PHP redirect header

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.

get the url from which the ajax call has been made in the ajax function

For example,
I have an ajax request from the page http://www.abc.com/xyz/1 to the function controlling ajax.
Is there any way except sending it through the parameters so that I could know that in the function I can fetch http://www.abc.com/xyz/1 and realise that the ajax request came from here?
If the server wants to know from which page, or the server's location, then you can use this in the end, if PHP:
die($_SERVER["HTTP_HOST"] . $_SERVER["HTTP_REFERER"]);
you can get request url by using below php code:
$request_url=( $_SERVER["HTTP_REFERER"]);

ajax request/php session vars

I'm fairly new to javascript/ajax so bear with me. I'm attempting to send a form field value as a POST variable to a php script, and have that php script set a session variable. I don't need to update any content on the page that I'm calling the javascript script from. So here is what I'm doing...
Here is my javascript stuff:
xmlhttp.open("POST","ajax/create_employee_session_handler.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("blah=1");
window.location="newpage.php";
And here is my php code (simplified):
session_start();
if($_POST)
{
$_SESSION['blah'] = $_POST['blah'];
}
And for some reason, when I redirect to the next page, I get no $_SESSION['blah']. Any suggestions on this would be appreciated.
You are starting the AJAX request, but you are not giving it a chance to finish (The "A" in Ajax stands for "Asynchronous" so when you do the location() the request is still running.) Chances are the Ajax script never gets called.
Put the part switching the page's location into the Ajax request's success callback to make sure it works out before redirecting.

Categories