Is there a way (in PHP) of detecting if the script call was caused by a jQuery load or not?
You might want to take a look at the HTTP headers your server receives.
For example, let's say I have this page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
</head>
<body>
<div id="test"></div>
<script type="text/javascript">
$('#test').load('temp.php');
</script>
</body>
</html>
And the temp.php script contains just this:
<?php
var_dump($_SERVER);
die;
When load
executed, the "test" <div>
will contain a dump $_SERVER
; and it will include this, among others:
'HTTP_X_REQUESTED_WITH' => string 'XMLHttpRequest' (length=14)
XMLHttpRequest
is the object that was used to create the Ajax request.
This means that you should be able to determine if the request was made with an AJax request, with something like this:
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
echo "Ajax";
} else {
echo "Not Ajax";
}
With this, you can determine if your page is called "usually" or with an Ajax request and decide whether to include layout or not.
BTW: This is exactly the solution that Zend Framework uses to detect Ajax requests for example .
source to share
If you are using Zend Framework you can use
// in one of your controllers
if ($this->getRequest()->isXmlHttpRequest()) {
// ...
}
http://framework.zend.com/manual/en/zend.controller.request.html
source to share