jqueryzend-frameworkzend-controller

How to dynamicly load of controller and action (Zend Framework + AJAX)?


I am trying to write a universal code for AJAX. I will show what I mean on the example of the e-shops admin panel. There are some things that we can edit, for example: categories, products, attributes. Each of those elements have its forms with input text and input checkbox fields etc. Saving is going to look like in Gmail, when you write something in the field it is automaticaly sent request to server. For each module (categories, products, attributes...) I have a controller and an action in it for example for edition editAction, for deleting deleteAction etc. It sometimes that I have in one view more than one form concerning controllers. In main layout I have a JS code:

$.ajax({
      url: 'http://localhost/fa.fronted/public/en/controller/action/',
      type: 'POST',
      data: 'websiteid='+iWebId+'&formid='+iFormId+'&formfieldid='+iFieldId+'&'+dataName+'='+dataValue,
      success: function(data) {
          if(data=='OK') {
              $('#ajaxmessage').text('All changes saved');
          }
          else{
              $('#ajaxmessage').text('Error: ' + data);
          }
      },
      error: function() {
          $('#ajaxmessage').text('Error');
      }
  });
}

I would like to use different URL addresses for different controllers and actions - I want to do this dynamicaly or manage it in the form.


Solution

  • function onChangeCallback(controller,action,formId){
        var data = getFormData(formId); 
        // or if you want to save by field $(this).val() will get you the input value
        // make the ajax request from the code posted in your question
    }
    
    <input type="text" name="username" onchange="return onChangeCallback('<?=$this->ControllerName?>'
    ,'<?=$this->actionName?>','myForm')" />