SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessible AJAX/PHP - IF Asynchronous... ELSE ....

    Just wondering if PHP can tell if a request comes in via an AJAX call rather than through a standard HTTP request.

    If the page is called via AJAX I just want to run a database call and load that data into a DIV on the page (which is already working fine) - but if it is a standard HTTP request I'd like to run the same request but also includes my headers, footers and the standard things.

    I'm just looking for accessibility if someone has JavaScript disabled.

    Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't believe you can but you can send a PHP get or post variable when using an ajax request to determine if ajax is being used, for example: index.php?ajax=true
    Kayzio - We don't hesitate, we accelerate.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would say your best bet is probably to include a $_GET variable that separates an AJAX call from one that's not. ?ajax=1 or something like that. In your script, check for that variable. There may be a better method, but that would be an easy way to do it at least.

  4. #4
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,188
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The easiest would be to just include a post/get variable with the ajax request, or if you want to do it more fancy modify the header and check the headers you receive before processing the request.

  5. #5
    SitePoint Member
    Join Date
    Dec 2004
    Location
    New Philadelphia
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also send a custom HTTP header with the request, then on subsequent requests check for the existence of the custom header.

  6. #6
    SitePoint Member
    Join Date
    Jul 2004
    Location
    us
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tristankelley View Post
    Just wondering if PHP can tell if a request comes in via an AJAX call rather than through a standard HTTP request.

    If the page is called via AJAX I just want to run a database call and load that data into a DIV on the page (which is already working fine) - but if it is a standard HTTP request I'd like to run the same request but also includes my headers, footers and the standard things.

    I'm just looking for accessibility if someone has JavaScript disabled.

    Thanks!
    A HTTP Request made via XMLHttpRequest and the regular one are the same.
    You should probably separate your output from your PHP for the type of content requested, and not just for AJAX.

    eg: If you need xml, then use a format=xml.

    or you could use HTTP Headers:

    Code:
    xhr.setRequestHeader("Accept", "text/xml");
    Then have your PHP code return the correct Content-Type based on the HTTP Request.

    PHP Code:
    header("Content-Type: text/xml"); 

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting idea on headers bucabay - I'll give it a shot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •