6 years ago
21
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 VIP membership
215 Posts
iliil
6 years 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
6 years ago
0
Level 2

Hi Michal,

Great reply & details! 👍🏻

Cheers,
Saba

6 years 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

215 Posts
iliil
6 years 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

6 years 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

215 Posts
iliil
6 years 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

1283 Posts
Bucklash
6 years 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

6 years ago
0
Level 4

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

6 years 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


215 Posts
iliil
6 years 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

6 years 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

6 years 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

12 Posts
jfquestiaux
4 years ago
6
Level 1

Hi,

I'm trying to implement an ajax call from a SEBLOD template, but with no success so far.

Here are my settings:
Seblod version : 3.18.1 - J! 3.9.15

The following S code is in templates/seb_minima/positions/les_guides/item/mainbody.php

<script>
jQuery(document).on('click', '.videoGuide', function(e) {
var mytoken= Joomla.getOptions("csrf.token");

jQuery.ajax({
context: document.body,
url: "index.php?option=com_cck&task=ajax&format=raw&file=includes/ajax.php&"+mytoken+"=1&referrer=template.seb_minima",
success: function(result){
console.log(result);
}
});
});
</script>

The PHP to execute is in templates/seb_minima/includes/ajax.php

I have this code inside to test:

<script>
console.log('Test OK');
</script>

I added this in templates/seb_minima/templateDetails.xml

</config>
<cck_ajax>
<files>
<file>includes/ajax.php</file>
</files>
</cck_ajax>
</extension>

The ajax calls return a 200 code but nothing happens in the ajax.php file.

I'm probably missing something but I can't find anything. Any help would be greatly appreciated.

215 Posts
iliil
4 years ago
5
Level 2

Hi jfquestiaux

Try to use echo 'Test OK'; instead of console.log('Test OK'); in your php  script.

BEst

Michal

12 Posts
jfquestiaux
4 years ago
4
Level 3

Thanks for the quick answer, but that does not change anything.

What could I put in the ajax.php to return something in the "success" function, so I would be sure it's doing something.

215 Posts
iliil
4 years ago
3
Level 4

Whatever you render in your PHP script will be returned. You can check your XHR request at  your browser tools for developers -> network -> XHR.  Look you can see the response there.

12 Posts
jfquestiaux
4 years ago
2
Level 5

That's what I thought but putting "echo 'Test OK';" in ajax.php does not return anything.

I have "<empty string>" as response of


success: function(result){
console.log(result);
}

12 Posts
jfquestiaux
4 years ago
1
Level 6

My impression is that the "checkAjaxScript" function is not working. Is there a way to enable it?

12 Posts
jfquestiaux
4 years ago
0
Level 7

OK. As far as I can tell, either you have to enabled something somewhere for thsi to work or the SEBLOD ajax function is buggy.

4 years ago
0
Level 1

Hi jfquestiaux,

You are on the good way.   

In the template.xml, you need to have the full path for the script file:

<file>templates/seb_minima/includes/ajax.php</file><br>

as for the URL in your javascript:

...&file=templates/seb_minima/includes/ajax.php&"+mytoken+"=1&referrer=template.seb_minima"


Regards.

47 Posts
StanislavR
1 year ago
0
Level 1

Hi,

after update 3->4 not work url

"/index.php?option=com_cck&task=ajax&format=raw&file=plugins/cck_field/rsvwishlist_btn/ajax.php&"+mytoken+"=1&referrer=plugin.cck_field.rsvwishlist_btn"

500 (Internal Server Error)

what should i change?

thanks

P.S.

in template

"/index.php?option=com_cck&task=ajax&format=raw&file=templates/TEMPLATE_NAME/ajax.php&"+mytoken+"=1&referrer=template.TEMPLATE_NAME"

works fine

Get a VIP membership