Home Top Projects Tutorials Articles Submit Project
 
  • .NET Frameworks
  • Java Frameworks
  • PHP Frameworks
  • Ruby Frameworks
  • Other Frameworks
  • Cool AJAX sites
  • Ajax Resources
  • Ajax Tools
  • JavaScript frameworks
 
     
    • 3D Tag Clouds Using jQuery
    • Scriptaculous Lists with PHP
    • Building an AJAX chat with Parancoe and DWR
    • How to add Google Maps to your website?
    • ASP.NET 2.0 Client side Callbacks Framework
    • Working with the Ajax Control Toolkit
    • Preventing Users Navigating Away from an AJAX.Net Page without Saving their Changes
    • Sample JavaScript Functions
    • Working with Exceptions using ASP.NET Ajax
    • Creating an Ajax Autocompletion Textfield with Dynamic Faces
    Home » Tutorials » Submit a form with Ajax

    Submit a form with Ajax

    The new release of CakePHP (RC2) comes with a completely rewritten AjaxHelper::form() function (with the disadvantage that it breaks existing code). Let’s look how to use it:

    $ajax->form(array('action' => '/controller/action'), 'post',
    array('update' => 'mydiv'));
    

    I think this code is self-explanatory (if not, please write a comment). What’s cool about this code is the fact that it will also work when you disable JavaScript. The form will submit the form data in both cases to /controller/action. That means you have to distinguish in your action whether it is called via Ajax or not, and then you have at least to select the appropriate layout. You can do it in the following way (thanks to nate for this hint):

    $headers = getallheaders();
    if (isset($headers['x-requested-with']))
    {
    $this->layout = 'ajax';
    }
    

    You can access the form data in both cases as you are already used to with

    $this->params['data']

    That’s it. It is really that simple :)

    Update (2006-03-16): The layout selection has changed (you still can use the approach described earlier), the new approach for doing the layout selection is:

    class YourController extends AppController
    {
    var $components = array('RequestHandler');
    function youraction()
    {
    ... // do something
    $this->RequestHandler->setAjax($this);
    }
    }
    
    This entry was written by cakebaker and posted on January 18, 2006 at 5:08 pmJanuary 18, 2006 at 5:08 pm and filed under ajax, feature, tip, cakephp. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Both comments and trackbacks are currently closed.
    « CakePHP RC2 is here!
    Dynamic menu with CakePHP »
     
    Read Full Tutorial
    Copyrights Reserved AjaxProjects.com 2006-2013, Powered by Enozom - Mobile Development Company -Privacy Policy