Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » Completely lost on Ajax forms

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 2387 Views
  • Shane Garelja
    Community Member
    18 Posts

    Completely lost on Ajax forms Link to this post

    I have not been able to figure out how to get a form to submit using Ajax. There is mention of using a FormResponse object here. But then there is no documentation for it.

    I have this in my Page_Controller class (NB: Haven't even started on DataObject stuff yet - just want to see this submitting via ajax):

    function ContactUsForm() {
    // Create fields         
    $fields = new FieldSet(
    new TextField('FirstName', 'Name', 'Name'),
    new EmailField('Email', 'Email', 'Email'),
    new TextField('Phone', 'Phone No.', 'Phone No.'),
    new CheckboxField('Brochure', 'Email me a brochure'),
    new CheckboxField('Call', 'Please call me'),
    new CheckboxField('Newsletter', 'Sign me up for e-newsletter')

    // Create actions
    $actions = new FieldSet(
    new AjaxFormAction('doContactUsForm', 'Submit'),
       new ResetFormAction('doCloseContactUsForm', 'Close')
    $reqdFields = new RequiredFields("Email", "FirstName");
    return new Form($this, 'ContactUsForm', $fields, $actions);//, $reqdFields);
    function doContactUsForm($data, $form) {   
    // your form actions
    FormResponse::update_dom_id('ContactFormLiner', '<p>DONE</p>');
    FormResponse::status_message('Done', 'good');
    // will automatically show the status-message if called by ajax, or redirect on a normal HTTP-request
    return FormResponse::respond();

  • Shane Garelja
    Community Member
    18 Posts

    Re: Completely lost on Ajax forms Link to this post

    Is this just a matter of copying the PageCommentInterface.js example or is there an easier "built-in" method?

  • Ingo
    Forum Moderator
    801 Posts

    Re: Completely lost on Ajax forms Link to this post

    AjaxFormAction is only used in specific cms interfaces, we're discussing deprecating it in a sapphire-framework-context, see http://open.silverstripe.com/ticket/2353

    PageCommentInterface.js is a good start, you just need basic PrototypeJS functionality to achieve what you're trying to do: http://prototypejs.org/api/ajax/request

    You can still use the FormResponse class, just make sure you evaluate the ajax-response rather than simply outputting it (we have a little wrapper called Ajax.Evaluator for this)

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.