Topic Closed
a2exfr
242 Posts
25.8K Downloads
2 Themes
10 Plugins

Sorry if this a stupid qwestion/

I have a form for example, how to sent it data  to plugin with ajax?

This work  for separeted file:

    $.ajax({
            type: "POST",
            dataType: "json",
            url: $( this ).attr('action'),
            data: $( this ).serializeArray(),
           success: function(data){ }

           });   

But I need to use data in plugin, with some gpeasy functions.

How to avoid 'Not an entry point...'  respond? (in joomla that was call to task in  controller  with ajax layout and here?)

4 years ago#8357

juergen
1.5K Posts
53.7K Downloads
16 Plugins
design, web development & visual effects

Add the PageRunScript section to your Addon.ini:

[PageRunScript]
script = MyAddonScript.php
method = MyAddonClass::PageRunScript

PHP: Add the PageRunScript hook function to your Addon class

class MyAddonClass {

// [...] your other hooks [...]

  function PageRunScript($cmd) {
    global $page, $addonPathData; // ... and any other globals you need

    // check the cmd querystring
    if ( $cmd == 'my_ajax_command' ) {
      $page->ajaxReplace = array();

      // do sth. awesome like ...
      $my_value = $_REQUEST['my_value'];
      $my_file = $addonPathData . '/values.php';
      msg( gpFiles::SaveArray($my_file, 'my_array_name', array($my_value) ) ? $langmessage['SAVED'] : $langmessage['OOPS'] );

      // define the AJAX callback
      $arg_value = 'yippie ki-yay';
      $page->ajaxReplace[] = array('myCallbackFn', 'arg', $arg_value);
      return 'return';
    }

    return $cmd;
  } /* endof PageRunScript hook */

}

JavaScript/jQuery:

$("#my_input").on("change keyup", function(e) {
  var href = jPrep(window.location.href)
    + '&cmd=my_ajax_command'
    + '&my_value=' + encodeURIComponent( $(this).val() );
  $.getJSON(href,ajaxResponse);
});

// attach your callback function to the $gp.response object

$gp.response.myCallbackFn = function(arg) {
  alert('my callback fn says: "' + arg + '"');
}

Hope it works. Like any quality advice, its completely untested ;-)

 

edit:/ Oh, and - if you want to do this stuff when not logged-in, you need to load the main.js, e.g. in the GetHead function

function GetHead() {
  global $dirPrefix;
  if (!common::LoggedIn()) {
    $page->head_js[] =  $dirPrefix . '/include/js/main.js';
  }
}

edit2:/ Master Josh may correct me in case anything or all of the above is stupid ;-)

Edited: 4 years ago#8361

juergen
1.5K Posts
53.7K Downloads
16 Plugins
design, web development & visual effects
BTW: It would be of great value if we had something like a monster skeleton addon with commented sample code of all the hooks and how to use them right.
I started such a thing several times but never finished it properly. Admittedly, there are several hooks that I haven't used myself yet.
Edited: 4 years ago#8362

a2exfr
242 Posts
25.8K Downloads
2 Themes
10 Plugins

thanks for reply!

your example, correctly send values to plugin and return  respond


jQuery111205082863859553484_1449475746039([{DO:"myCallbackFn",SELECTOR:"arg",CONTENT:"yippie ki-yay"}]);
 

so

 $gp.response.myCallbackFn = function(arg) {
  alert('my callback fn says: "' + arg.CONTENT + '"');
}

No need to load the main.js work as is, loggen and unlogged.

and it's two times launched (may be it's my mistake, at this point can't find where :)

may be $gp.response is not for usage like this?

Anyway thanks, with this it pushed to work!

If exists more correct way to send and get ajax data in plugin, I hope Josh give us some info.

 

4 years ago#8370

a2exfr
242 Posts
25.8K Downloads
2 Themes
10 Plugins

found that possible to do another way.

// send data to same page where gadget(plugin) is placed   

$.post("", {"action":ajax_call,"data":data}, function (result) {
  

// find something that plugin return depend on $_REQUEST['action']; html or  Json placed in data-atribute in hidden element      

     var html = $.parseHTML(result);

  console.log($(html).find( '#result' ));
           
            });

not test yet with json, but think everthing will be ok

 

Edited: 4 years ago#8373

juergen
1.5K Posts
53.7K Downloads
16 Plugins
design, web development & visual effects

If I remember correctly, Simple_Event_Calendar also AJAXes the calendar Gadget, maybe also take a look there.
.

4 years ago#8388

Topic Closed

 

News

elFinder 2.1.50 in Upcoming Release
12/28/2019

A new release for Typesetter is in the works with a lot of improvements including the ... Read More

Typesetter 5.1
8/12/2017

Typesetter 5.1 is now available for download. 5.1 includes bug fixes, UI/UX improvements, ... Read More

More News

creisi productions

Dienstleistungen von creisi productions, Luzern (Schweiz): * Konzeption, Planung und Erstellung Ihres Internet-Auftritts * Betreuung und Aktualisierung/Pflege Ihrer Website * ...

Find out more about our Provider Spotlight

Log In

  Register