ajax from edit.php

Permalink
Hi all,

Is it possible to communicate in ajax mode to a controller of a package from his add.php or edit.php ?

In other words, it is possible to use the great feature "action" from the file add & edit? I know the general procedure (very well explained in the documentation) but I can not retrieve the url :

I try :
$v = View::getInstance();
$postUrl = $v->action('test');

But nothing..
I suppose that i need to keep first the controller?

sebastienj
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Ajax responders should be in "tools" files. And you can only get their URL from in your controller (not in add.php or edit.php). So you need to get the tools url in your controller's add() and edit() functions, like this:
private function add() {
   //Can't use the $this->action() method from add or edit forms, so we have to use tools files to respond to ajax calls.
   $th = Loader::helper('concrete/urls'); 
   $this->set('my_ajax_url', $th->getToolsURL('my_ajax_filename', 'my_package_handle')); //<-- do NOT include ".php" extension on end of filename
}


Now you add/edit.php file has that $my_ajax_url available to pass into javascript functions, for example:
$.ajax({
   url: '<?php echo $my_ajax_url; ?>',
   dataType: 'html',
   success: function(response) {
      //do stuff here;
   }
});


If you want to have your javascript in your auto.js file (like you should -- keep it out of the add/edit.php which should mostly be just html), you need to declare a javascript variable in add/edit.php for that ajax url, then you can access that variable from the auto.js file.

Check out the code in "Simple Image Gallery" in the marketplace to see an example of how I've done this (I used it to dynamically populate the list of filesets that the user can choose from in the add/edit form).

-Jordan
JSA1972 replied on at Permalink Reply
ShadowDancer replied on at Permalink Reply
can this be done with block tools as well as package tools? In my controller.php I have the following add function declared.
public function add()
   {
      $th = Loader::helper('concrete/urls'); 
         $this->set('my_tools_url', $th->getToolsURL('getinfo'));
   }


and in my auto.js I have
var url = '<?php echo $my_tools_url; ?>';


but when I test the code by supplying the variable called url as an argument to the JQuery alert method
alert('Tool found at: "'+url+'"');


it simply prints "Tool found at "<?php echo $my_tools_url; ?>""

I'm trying to pass the path to my tools directory as a variable to the auto.js file instead of hardcoding it.
JohntheFish replied on at Permalink Reply
JohntheFish
If you have a play with my Ajax lessons howto and associated addon it shows all permutations of ajax mechanisms in add, edit and view.
ShadowDancer replied on at Permalink Reply
I've had a look and can't seem to find where you're grabbing the tool path. Specifically in the Ajax call that is.

if i use
public function add(){
      $this->set_block_tool('getinfo');
   }
   private function set_block_tool($tool_name){
      $tool_helper = Loader::helper('concrete/urls');
      $bt = BlockType::getByHandle($this->btHandle);
      $this->set ($tool_name, $tool_helper->getBlockTypeToolsURL($bt).'/'.$tool_name);
   }


and then echo the value of $getinfo in add.php it gives me the path I'm looking for I just can't seem to get this variable into my auto.js file
JohntheFish replied on at Permalink Reply
JohntheFish
The set() is only visible in the add/edit form.

An easy way to pass it to auto.js is to attach it as a data attribute to a dom element (in the add/edit form), then in auto.js to read that data attribute.
ShadowDancer replied on at Permalink Reply
that feels a little like cheating lol but if it's the only way it can be done then it's the only way and I'll do it :)
Thanks John you're a legend
sebastienj replied on at Permalink Reply
sebastienj
Ho! Jordan thank you very much for these clear and precise explanation. I was already using a lot of ajax in my block but not as cleanly.
I'm going to see the gallery of which you speak.