Jump to:

22975 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Call ajax to form action

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 318 Views
  • Craftnet
    Avatar
    Community Member
    58 Posts

    Call ajax to form action Link to this post

    Hi, I have form like extend
    I add to function

    if(Director::is_ajax()){
             return $this -> renderWith("Footer");
          } else {//DO SOMETHING}

    How to call in js to this function from Form ("AddObiekt")

    When in JS add url: ajax/AddObiekt - he call to Page Controller
    I wont that AJAX in JS call to function AddObiekt from class AddObiektForm extends Form

    AddObiektForm

    <?php
    class AddObiektForm extends Form {
          
       function __construct($controller, $name) {
          
          $title = new TextField('Title', 'Tytuł');      
          //OTHER STAFF
          
          $fields = new FieldList(array(
             $title,
             //OTHER STAFF
          ));
          
          $validator = new RequiredFields(
    'Title'
             //OTHER STAFF
       );
          
          $actions = new FieldList($search = new FormAction("AddObiekt", _t("AddObiektForm.ADD", "Dodaj obiekt i przejdź do dodawania zdjęć")));

          parent::__construct($controller, $name, $fields, $actions, $validator);
          
       }

       function forTemplate() {
          return $this -> renderWith(array(
             $this -> class,
             'Form'
          ));
       }
       
       public function AddObiekt($data, $form) {
                
          if(Director::is_ajax()){
             return $this -> renderWith("Footer");
          } else {
          
          //VALIDATE BY PHP
          
          if ($Obiekt) {
             //DO SOMETHING
          } else {
             //DO SOMETHING         
          }
          }
       }

    }

    JS

    function tt() {
             $('#AddObiektForm_AddObiektForm_action_AddObiekt').click(function(e) {
                 e.preventDefault();
             
             var $form = $("#AddObiektForm_AddObiektForm");
             
                var obiekt_id = $(this).data('obiektid');
                loading_show();
                $.ajax({
                   type : "POST",
                   url: "ajax/AddObiekt",
                   data : $form.serialize()
                }).done(function(msg) {
                   $('#sidebarMenuProfil').html(msg);
                }).error(function(msg) {
                   alert('Błąd! Proszę spróbować ponownie lub skontaktować się z administratorem');
                });
             });
          }
          $(tt);

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