This site uses cookies and similar technologies.

If you don't change browser settings, you agree to it.

I understand
1 year ago
12
Topic

Hi

Is there some good / recommended way to perform some ajax calls with seblod ?

Actually I would need to trigger a PHP code field upon a JS event. When the user clicks a special div I must trigger a PHP code. 

I'm sure this is something that could benefit from a best practice guide, couldn't it ?

thanks

cyril

Get a Book for SEBLOD
173 Posts
iliil
1 year ago
1
Level 1

Hi Cyril

My strategy is to use existing solutions as much as possible. So I am using the SEBLOD way and so far I am very happy with it. (I also appreciate the seblod changelog, when using their functions :)

For SEBLOD AJAX URL I use following params:

  • option=com_cck
  • task=ajax
  • format=raw
  • file=( URL to your PHP script )

There is also a token parameter that is provided by the global javascript object Joomla (method getOptions("csrf.token"),

  • "token code"=1
Since SEBLOD 3.16.0 ajax task requires scripts to be allowed by extensions. The URL must contain a parameter referer which value refers to the Joomla extension(component, plugin, template ) that allows your ajax task. For example:
  • referrer=plugin.cck_field.the_name_of_your_plugin
The selected extension installation XML file must contain the tag cck_ajax with the URL to the target php file  (identical to the value of the URL parameter file) as a reference e.g.
<cck_ajax>
<files>
<file>plugins/cck_field/the_name_of_your_plugin/tmpl/form.php</file>
</files>
</cck_ajax>
Besides, you can add some custom parameters that you need to perform your PHP script. In your PHP file you can get the parameter using Jinput
JFactory::getApplication()->input->get( 'name_of_your_param' );
For ajax call I use standard jQuery:
$.ajax({
context: document.body,
url: "url described above",
success: function(result){
//do something with the result
}
})
I hope it helps :)
Cheers
Michal
1 year ago
0
Level 2

Hi Michal,

Great reply & details! 👍🏻

Cheers,
Saba

1 year ago
5
Level 1

Hi Michal

thanks for this. However I need to pass this to our devs since I'm not a dev myself. 

As I understand you we have to developp some custom component anyways. 

First you wrote:

For SEBLOD AJAX URL I use following params:

  • option=com_cck
  • task=ajax
  • format=raw
  • file=( URL to your PHP script )

SO I would understand that in 'file' you provide your custom PHP script 

but then you talk about  aplugin we have to write

 plugin.cck_field.the_name_of_your_plugin

Would it be possible you write a full tutorial describing the launch of some basic PHP script triggered by some DOM manipulation with seblod ?

(I would pay for it :) )

thanks

cyril

173 Posts
iliil
1 year ago
4
Level 2

Hi Cyril

I don't think it is necessary to develop a special extension in order to exploit SEBLOD ajax functionality.  The file is just a PHP file. On the other hand, yes since SEBLOD 3.16.0 the PHP file must be checked in a Joomla extension XML.  

If you look at the method checkAjaxScript of JCckDevHelper class

libraries\cms\cck\dev\helper.php

You'll find all the options where you can put the file reference. You can add the reference code to a templateDetails.xml of your custom template, for example.

I try to find some time for writing some tutorial

Mic

1 year ago
3
Level 3

Thanks for the additional info. I'm waiting for your tutorial

I badly need some 'official' guide to make ajax calls with seblod

thanks

cyril

173 Posts
iliil
1 year ago
1
Level 4

..well, I am not the right competent person to write "official" guides as I am just a SEBLOD user. I only can share my personal experience. I try to prepare something over the weekend.

Cheers

Michal

1219 Posts
Bucklash
1 year ago
0
Level 5

Hi

Jumping in to this (great) post... a suggestion:

For any tutorials or tips write them as a forum post, that way you can edit them as time goes by i.e. code updates or typos.

Also, there is then the potential for comments to be added with examples provided by any kind folk (as seen in php documentation).  

Jon

1 year ago
0
Level 4

Thanks every one for your great contributions and to keep common respect.

1 year ago
3
Level 1

Hi 

I'm trying to create the simplest ajax with seblod example here:

Here is what I did:

  • Create a includes/ajax.php file
  • put a JS code in a content view with the following lines :

var mytoken= Joomla.getOptions("csrf.token"); 

$.ajax({ 

context: document.body, 

url: "index.php?option=com_cck&task=ajax&format=raw&file=includes/ajax.php&"+mytoken+"=1&referrer=templates.protostar", 

success: function(result){ 

alert(result); 

}

})

  • in the templates/protostar/templateDetails.xml file I added the lines at the end of the file :

<cck_ajax> 

<files> 

<file>includes/ajax.php</file> 

</files>

</cck_ajax>

  • in the includes/ajax.php file I wrote some basic code that sends an email (with JFactory::getMailer()) to test the call

I tested the ajax.php code with the url http://mydomain/includes/ajax.php : I recieve the email.


However when I display the content view of my article the JS code alert displays no error but no  email is sent.

thanks


cyril


173 Posts
iliil
1 year ago
2
Level 2

Hi Cyril

It's hard to say, from what you write, everything seems ok. Maybe, for now, ignore the sending emails stuff and try to make the ajax work first.

  • instead of sending email, just echo "hello world" in your ajax.php;
  • in the success method of your ajax object, output result in the web console -> success: function(result){ console.log(result) }
  • in templates/protostar/templateDetails.xml make sure that the is inside of tag (...not at the end of the file)

Now enable the web console and perform the ajax call, check if you output "hello world". This should work, if not

  • Check your XMLHttpRequest in the network section of your web console, look at the status, doublecheck the parameters and look at the response.

Cheers

Michal

1 year ago
1
Level 3

Hello Michal

First of all I really appreciate your help, thanks again. I did as you wrote :

  • the /includes/ajax.php file has been simplified to

echo("hello world");

  • the referrer in the Templatedetails file is before the last closing tag :

 </config> 

<cck_ajax> 

<files> 

<file>'/includes/ajax.php'</file> 

</files> 

</cck_ajax> 

</extension>

 

  • In the web console I can't see any return (as there weren't with the alert(result); method )
  • in the web console (network section)  I checked the Status Code:200 OK
  • I also doublechecked the parameters :
  1. option: com_cck
  2. task: ajax
  3. format: raw
  4. file: '/includes/ajax.php'
  5. dbab1331092bcc304fe62de8f9167c81: 1
  6. referrer: templates.protostar 
  • in the web console (network section) there is nothing in the response tab

Thanks for your help

Cyril

1 year ago
0
Level 4

Ok I found the error

According to the libraries\cms\cck\dev\helper.php file in the ajax url the referrer has to be one of the following case :

component / plugin / processing / template / variation


In my case it has to be 'template' instead of 'templates" !!


now the ajax call is working as expected!


thanks a lot


Cyril

Get a VIP membership