Jump to:

2012 Posts in 1438 Topics by 621 members

Form Questions

SilverStripe Forums » Form Questions » Display form via ajax response

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

Page: 1
Go to End
Author Topic: 1584 Views
  • esakrielaart
    Avatar
    Community Member
    56 Posts

    Display form via ajax response Link to this post

    Hello,

    I request some help with the following. I was to display a form using ajax when a user clicks a certain link. I managed to get it working when I just let ajax respond with some plain text, however not with a form. I tried the following:

    ajaxSnippet.ss (template file)

    $Form

    Page.php (code file)
    $form = new Form($this, 'Form', ....);
    return $form->renderwith('ajaxSnippet');

    Where the Form(....) is defined as normal, with just a textfield and a submit button. I think my error is in the template file, that I might need to reference not to $Form but something else, only I can't think of it.

    Any help appreceated,
    Maurice

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Display form via ajax response Link to this post

    Have one function for the form (which you already have) and then have a separate method for sending the form back.

    public function FileUploadForm() {
       $fileUploadField = new FileUploadField('ProjectFile', "Upload a file");
       $fileUploadField->setUploadFolder($this->projectFolder);
       $fields = new FieldSet(
          new HiddenField("ID"),
          new HiddenField("ParentID"),
          $fileUploadField
       );
       
       $actions = new FieldSet(
          new FormAction('doUploadFile', 'Upload')
       );
       return new Form($this, "FileUploadForm", $fields, $actions);
    }

    function ajax_file_form() {
       $form = $this->FileUploadForm();
       return $form->forTemplate();
    }

    In this case I have a function called FileUploadForm which constructs a form and returns it in a normal manner. The function ajax_file_form() then calls that function and returns the form as a string with the $form->forTemplate() method.
    If you want a different template for the actual form then use $form->setTemplate() to set a new template. You have to have a function (as far as I know) that returns a new Form for the form handling to work properly. If I'd wanted to just print that form in a template I'd write $FileUploadForm to show it.

  • esakrielaart
    Avatar
    Community Member
    56 Posts

    Re: Display form via ajax response Link to this post

    Thank you, that worked for me (the forTemplate() method). Have a nice day.

  • esakrielaart
    Avatar
    Community Member
    56 Posts

    Re: Display form via ajax response Link to this post

    Hi,

    it looks like the normal way to handle a form does not work when it is called using ajax? I have in my pageController:

    function showProductDetails($action) {
        $productNumber = Director::urlParam("OtherID");
        $referer = Director::urlParams();
        $Link = $referer["URLSegment"]."/".$referer["Action"];
        if($this->isAjax) {
          // Get product details
          $details = DataObject::get_one("Article","ID = $productNumber");
          // Get product contents
          $contents = DataObject::get("ArticleChilds","ArticleID = $productNumber AND stock > 0");
          if(!empty($contents)) {
           $map = $contents->map('ID','Name');
           $fields = new FieldSet(
              new DropdownField('ID', 'Maak een keuze:', $map)
           );
          } else {
           $fields = new FieldSet(
           );
          }

          $actions = new FieldSet(
             new FormAction('doSubmitNewItem','Submit')
          );

          $form = new Form(
             $this,
             'showProductDetails',
             $fields,
             $actions
             );
          return $form->forTemplate();
        } else {
          return array();
        }
       }

       function doSubmitNewItem($data,$form) {
        // Save the form data

        return;
       }

    However this does not in any way I try below the // Save the form data, actually save the form data. Do I make use of incorrect references to the save-function?

    Thank in advance,
    Maurice

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Display form via ajax response Link to this post

    That's why I talked about having a separate function that grabs the form and returns the output for AJAX requests. Your form function has to always return a form object otherwise the form submissions won't work. So the AJAX function calls the form function and returns forTemplate() and the form function returns the form. If you do it like that everything should work.

    1584 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.