Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » Ajax and Forms

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

Page: 1
Go to End
Author Topic: 1302 Views
  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Ajax and Forms Link to this post

    I am starting to look at AJAX and forms. I am having no problem retrieving records and loading forms into the divs and so on. However, I am having trouble figuring out how to submit a form date using AJAX and thus prevent a full page reload.

    Any help on how this is achieved in SS would be much appreciated. Some example code event better!

    Thanks in advance.

    Paul

  • Martijn
    Avatar
    Community Member
    271 Posts
  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Re: Ajax and Forms Link to this post

    Thanks Martin. I have followed these and all all seem to be working. My problem is with this part of the guide from the form:recipes.

    class RegistrationForm_Controller extends Page_Controller {
    function doform($data, $form) {
    // your form actions
    FormResponse::update_dom_id('myForm', '<form>myformcontent</form>');
    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()
    }
    }

    I note the line "will automatically show the status-message if called by ajax, or redirect on a normal HTTP-request"

    I can not get my form to do anything other than the normal HTTP-request post submit.

    Here are the relevant code snippets. I am sure I am missing something obvious, I should maybe point out that I am new to the AJAX world! (if that was not obvious already!).

    Any help much appreciated.

    Thanks in advance.

    The form:

       function Studentedit() {
       
          $goeditid=Session::get('goeditid');
    //      debug::show($goeditid);
          $student = DataObject::get_by_id('Student',$goeditid);       

    //      $student = DataObject::get_by_id('Student',$_GET['id']);       
          $actions = new FieldSet(
             new FormAction('doUpdateStudent','Save')
             );

          if ($student) {
             $form= new StudentForm2($this,'StudentEdit',$actions);
             $form->loadDataFrom($student);
             return $form;
          }
          else {
             return $this->httpError(404,'Sorry, student does not exist');

          }

       }

    The submit method:

       function doUpdateStudent($data,$form) {

    $student = DataObject::get_by_id('Student',$data['ID']);

       if ($student) {
          $form->saveInto($student);
          $student->write();
       }
       
       if($this->isAjax) {
          FormResponse::update_dom_id('ajaxEditForm', '<p>DONE</p>');
          FormResponse::status_message('Done', 'good');
       }
       else {
          return Array();
       }
       
    return FormResponse::respond();
    }

  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Re: Ajax and Forms Link to this post

    Can anyone help here. Not looking for the crown jewels...just a little nudge in the right direction.

    Thanks in advance for any assistance.

    Paul

    1302 Views
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.