How to get an AJAX call in component's administrator side

Joomla Asked by LaioZatt on September 5, 2021

I added a component to my site and it accesses my DB via an AJAX call. In my ‘tmpl’ folder, I have a ‘default.php’ file which makes an AJAX call that is received by view.json.php just outside that folder. I really struggled to made that work, but I got it running by following the Developing an MVC Component documentation.

Now I want to do the same thing with my administrator part. I have a helloworld.php file inside my administrator > components > com_helloworld and I want to make an AJAX request. However, adding a view.json.php next to that file doesn’t catch the AJAX call. The administrator files look like this:
component administration side files

and it doesn’t work using:

    type: "POST",
    url: "#",
    data: {
        [token]: "1",
        task: "savetasks",
        format: "json",
        json: valuesJson
    success: (html) => {
    error: (err) => { 

But the same thing when done in the ‘site’ side works perfectly; the files look like this: component client side files

The only real difference is that on the administrator side, I call the AJAX from helloworld.php and in the client I call it from default.php.

The way I get my AJAX call in the frontend is by using:

    type: "POST",
    url: '#',
    data: {
        [token]: "1", //i have the token saved in an input at the page just like the tutorial.
        task: "AnythingIInsertHereWorks",
        format: "json",
        json: stuffIWantToPass
    success: (html) => { 
        //i do stuff on success here
    error: (err) => { //alert error

And I get this at the view.json.php file. The tutorial says it goes there because it is waiting to catch a JSON request, which I’m sending. The task name in the AJAX call can be whatever and the token I am saving in an input at the page. This works fine, in my view.json.php file I do stuff like:

$json = ($_POST['json']);

$user = JFactory::getUser();

$id = ($user->id);

$db = JFactory::getDbo();

$query = $db->getQuery(true);

$query = "insert into user_task values $json on duplicate key update id = values(id), user_id = 
values(user_id), task_id = values(task_id), completed = values(completed), enabled = values(enabled), 
supplier = values(supplier)";


This way the user can change inputs on the front-end and when they click save, I call this AJAX to save things into the tables I created. And this does the job. I don’t understand, however, how do I do the same thing on the administrator?

I have the html with inputs and such on my helloworld.php file in the administrator part, just like shown. I get stuff from the DB and show it on the page as normal using $db = JFactory::getDbo(); and such. I don’t really know how to save into the database from there. I just want to use AJAX to save the data that the administrator edited and wants to save. But adding a view.json.php file in there doesn’t "catch" the AJAX json call. I don’t want to use a standard form submission since I don’t want to reload the page.

I hope this question is not confusing. I can give any other pieces of code and such.

I’ve asked the same question in the Joomla! forums and on Stack Overflow, but didn’t manage to solve it yet.

This is Joomla! 3.x as far as I’m aware. Also if someone would be so kind, I could join a voice chat in discord or other.

Add your own answers!

Related Questions

Disabling cache on specific part of page from above comment

2  Asked on September 14, 2020 by ruchika


Custom Category Field

1  Asked on August 18, 2020 by grant-g


How do I pass _JEXEC to a php file?

1  Asked on August 6, 2020 by firefighter


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP