Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP if statement if the page was loaded with ajax

Tags:

jquery

php

I have a page that loads with a div populated from an include file (pullList.php), but if you click a button (let's say a "re-load" button) the content of the div is reloaded with that same file (pullList.php).

The problem now is that when the page is loaded via ajax (with jquery) I need an include file (function.php) inside the pullList.php file which is already included on the page.

So, ideally I'd like to be able to write a statement that says

if(the page was loaded with ajax) {
       include(function.php);
}

That way the function.php file will only be loaded once, and if the page is requested again via ajax, it has the right functions to display the content properly.

I've tried using include_once, but didn't work - had the same problem. Any suggestions?

Thanks!

like image 473
Andelas Avatar asked Feb 20 '11 10:02

Andelas


3 Answers

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  include(function.php);
}
like image 185
delphist Avatar answered Nov 06 '22 07:11

delphist


function isAjax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
        ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
}

Source : http://snipplr.com/view/1060/check-for-ajax-request/

like image 22
Intrepidd Avatar answered Nov 06 '22 08:11

Intrepidd


Check the $_SERVER["HTTP_X_REQUESTED_WITH"] contained xmlhttprequest

like image 2
Shaun Hare Avatar answered Nov 06 '22 09:11

Shaun Hare