SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making ajax requests to a controller

    I was wondering how people are integrating ajax into the web apps.

    For instance, one part of my site is a contact page, with the form action posting to ContactController::submitForm, which validates a user-supplied email address (among other things) and sends the form.

    Now an aspect i think ajax works well is input validation, and i'd like to verify the users email address before they post the form. The only way i can currently do this is to wrap my email validation class into a separate file to be used for, and only for, the purpose of ajax requests. You see the problem - i'm starting to fragment my codebase by creating different files for different types of input.

    So i was wondering if it's a bad practice for a controller to call methods on itself. For example, in ContactController::submitForm, validation of the email address could occur (and be delegated to from submitForm) in ContactController::validateEmail. This way if the form is posted, the email is validated by php, but i can also point my ajax request directly to ContactController::validateEmail.

    What are your opinions on this?

  2. #2
    Twitter: @TimIgoe silver trophy TimIgoe's Avatar
    Join Date
    Feb 2005
    Location
    Blackpool, UK
    Posts
    1,056
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    I've been wondering this myself - currently I've gone for a selection of seperate scripts but I don't like this method - it seems cluttered.

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rails takes the approach of responding to different types of requests by mime/type (I believe the Prototype javascript library sends special HTTP header in its AJAX requests). Most controller code should be the same no matter how its being invoked, its just the response that varies. Here's how Rails does it:

    Code:
    def update
      @record = SomeModel.find(params[:id])
      @record.update(params[:record])
      
      respond_to do |wants|
        wants.html { # render some html }
        wants.js     { # return some response to an ajax request, perhaps just some data or a JSON encoded string }
        wants.xml  { # send back some XML with the appropirate mime type }
        wants.whatever { # custom responds types }
      end
    end
    Now PHP doesn't have blocks that makes the above code so succinct but I'm sure something similar could be accomplished with PHP.

  4. #4
    Twitter: @TimIgoe silver trophy TimIgoe's Avatar
    Join Date
    Feb 2005
    Location
    Blackpool, UK
    Posts
    1,056
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    Responds to the type of request by mine/type - but I thought that was set to say what the returning data is. I didn't think the browser requested a certain type of data?

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah sorry its not the mime/type, I meant the HTTP Accept header. Here's some more info:

    http://www.loudthinking.com/arc/000572.html

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually it is the mime-type all right. The field is called Accept when requesting and Content-Type when responding, but the value is a mime-type in both cases. The reason for calling it Content-Type is because that's what it is called in emails, so to be consistent with that already established standard, they choose to call it the same in the HTTP protocol.

    Just like your controller can (and should) dispatch to different functions on different HTTP-methods (GET/POST), so can it dispatch to different functions, depending on the Accept header. I'd normally use the same URL for ajax-type callbacks, but send a custom Accept header to indicate the type of response expected. This could be something like application/x-ajax-html for HTML-fragments, or application/x-ajax-json for JSON type response etc. There is no standard content-type for these different types of response, so everybody seems to be making up their own slightly different solutions. The Prototype library for example, doesn't even use the Accept header - instead, they made up their own header field. There was some effort to standardize this matter, but I don't think anything ever happened.

  7. #7
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps the solution I have used in the past is too simplistic for you, but I would just insert an 'ajaxCall' variable in the javascript to be POSTed with the other data I passed, and would just do a simple if check for that variable like so:
    PHP Code:
    if(isset($_POST['ajaxCall']))
    {
        
    // Ajax response code
    }
    else
    {
        
    // HTTP response code

    It's simple, elegant, and easy to follow; and, it just works.

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Having a blast using the Symfony framework which handles all this for me. The way that framework handles it is to treat AJAX requests the same as any HTTP request -- they get routed to a module then an action within a module. The only difference is Symfony detects it's an XMLHttpRequest as kyber described and skips decorating the template of that action with the module's layout, so all that's returned is that action's output alone. Prototype/Scriptaculous handle all the actual JS work.

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > It's simple, elegant, and easy to follow; and, it just works.

    Why not simple have the AJAX request go to it's own specific Action, on the required Controller?

    Maybe I'm missing something though, since I've not got around to using AJAX yet...


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
  •