Trying to build a plugin for database communication through php

In my website I need to get data from database for display. Earlier I had an ajax call for that, but now it’s giving 403 permission error (due to .htaccess). From a discussion I found that I would need to develop a plugin for that. But I am still not sure how would I particularly initialize the plugin through a button click event from java script/ jQuery instead of onPageInitialized event.

You wouldn’t. The plugin is run when the page is viewed. Either do proper Ajax with JavaScript that contacts MailChimp and returns the results or tie a custom plugin to a custom route that is called by the button press. The plugin would take the data passed to it, talk to MailChimp, and then output whatever you want.

Consider the How do I interact with external APIs? section of the “Learning by Example” section of the plugin cookbooks page.

Thanks for your reply. I am sorry, but I didn’t get what you meant by proper Ajax . What I am doing now is, I have defined the database details in a db.yaml file and put my php files outside the grav folder. So with the ajax call it is working. However I am feeling that I have messed up the Grav way of doing things.

Then we’re talking past each other. Ajax is the use of JavaScript to make asynchronous calls to external APIs so you don’t have to refresh a page. If I wanted to talk to MailChimp (which I believe is what you initially were talking about), I would tie a JS function to the button click that grabs whatever data is needed, calls MailChimp, gets a response, and displays it. All of this has nothing to do with Grav. Alternatively, you can create a plugin that ties itself to a special route (like the Login or Admin plugins). When you submit the form, it goes to that route. The plugin does it’s job and returns the result.

If all you want to do is access data on your file system, the plugin cookbook has a section called How do I read from and write data to the file system? that can show you how to do that.

It’s ha rd for me to be more specific because I don’t know what you’re actually trying to accomplish. Your Gitter discussion suggests that you’re trying to subscribe people to a mailing list. Let me know if this is incorrect.

No. I am not talking about MailChimp. The discussion in Gitter was not mine, I just found it. All I want is to get some data from my database. For your understanding, in my existing website, this page requires data from database in json format.

If you just need to load some JSON, then the Import plugin is all you need. You’re going to need the updated version, though. The original maintainer is nonresponsive, so I will be asking Grav to make my fork authoritative next week (as per the protocol ). You can do a manual install from my fork for JSON functionality. You could of course also write a plugin if you need something more complex.

OK. I have just checked your plugin. But it is for static json from data folder. I rather require dynamically generated json from database, which may change/ update as per user interactions.
I think any suggestion for a new plugin for ajax call from database will be helpful.

Then I only see two options:

  1. You create a standalone database frontend that you query via Ajax (as you note, that interface would have to be external to Grav). It would be up to your JS code to determine how the results are populated into the final page.

  2. You create a plugin that talks directly to the database. Plugins run when a page is rendered. You can’t invoke a plugin by click. So your plugin would have to live on a separate route that form data gets submitted to. It would do whatever work it needs to do and then return appropriate output. Unlike Ajax, this would require a page refresh.

There are also numerous data visualization frameworks out there if you wanted to load static JSON into a table that users then search/sort/filter. Tablesorter is one such framework. How you approach it depends on what users need to do with your data.

Thanks again for your reply. I am actually using D3.js for my visualization.
Among the options you have mentioned, I feel the first one is only applicable for me, as I can not force the user to refresh the page every time. My ajax call now looks like

request = $.ajax({
        url: "../phpscripts/data.php",
        type: "post",
        data: data,
	dataType: 'json',
	success: function (response) {},
	error: function (request, status, error) {}
    });

I just thought this is not a good/ best practice for Grav. Maybe in future there will be some options available for plugins to be invoked by button click event. For now, I will continue with database interface from outside Grav.