AJAX call in administrator

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
laiozatt
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Jan 27, 2020 8:24 pm

AJAX call in administrator

Post by laiozatt » Thu Jul 02, 2020 2:15 pm

Hello, Joomla newbie here. I added a component to my site and made it acces my DB with an AJAX call, i have a 'default.php' in my 'tmpl' folder, which calls an AJAX that is received by the view.json.php just outside that folder. I really struggled to made that work but i got it running following the 'create a mvc component' tutorial. Now i want to do the same thing with my administrator part. I have a 'helloworld.php' inside my administrator > components > com_helloworld and i want to make an AJAX request, but adding a view.json.php next to that file doesnt catch the AJAX call. The administrator files look like this: Image and it doesnt work using:

Code: Select all

$.ajax({
		type: "POST",
		url: "#",
		data: {
			[token]: "1",
			task: "savetasks",
			format: "json",
			json: valuesJson
		},
		success: (html) => {
			console.log("success");
		},
		error: (err) => { 
			alert(JSON.stringify(err));
		}
	});
But the same thing when done in the 'client' side, works perfectly, the files look like this: Image

The only real difference is that on the admin side i call the ajax from the helloworld.php and in the client i call it from the default.php

Can someone help me make this work? I just need to be able to send some stuff to my DB, but it feels like hell doing it in joomla. Thank you!
Last edited by toivo on Thu Jul 02, 2020 2:48 pm, edited 1 time in total.
Reason: mod note: moved from 3.x General Questions

 
SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1761
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: AJAX call in administrator

Post by SharkyKZ » Thu Jul 02, 2020 6:52 pm

Impossible to tell without knowing component's code. But if it follows the same conventions in frontend/backend, difference is that in site part the view file is placed correctly in views/[view-name] directory. In administrator the view file is placed in component root and normally would not be detected as a valid view.

laiozatt
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Jan 27, 2020 8:24 pm

Re: AJAX call in administrator

Post by laiozatt » Thu Jul 02, 2020 9:30 pm

SharkyKZ wrote:
Thu Jul 02, 2020 6:52 pm
Impossible to tell without knowing component's code. But if it follows the same conventions in frontend/backend, difference is that in site part the view file is placed correctly in views/[view-name] directory. In administrator the view file is placed in component root and normally would not be detected as a valid view.
Thank you for the reply! So, if i get my 'view' code from my helloworld and put it inside the view folder, creating a file like default.php, would then i be able to pass an AJAX to the json file? I can give u any code parts u need, im really desperate =S

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1761
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: AJAX call in administrator

Post by SharkyKZ » Fri Jul 03, 2020 5:30 am

You don't need a layout file (i.e. default.php), it's for HTML views only.

I'm not sure if you need a JSON view at all. At least currently your AJAX code calls savetasks() method of your main controller (in controller.php file).

laiozatt
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Jan 27, 2020 8:24 pm

Re: AJAX call in administrator

Post by laiozatt » Fri Jul 03, 2020 1:58 pm

SharkyKZ wrote:
Fri Jul 03, 2020 5:30 am
You don't need a layout file (i.e. default.php), it's for HTML views only.

I'm not sure if you need a JSON view at all. At least currently your AJAX code calls savetasks() method of your main controller (in controller.php file).
The way i get my AJAX call in the frontend is by using:

Code: Select all

$.ajax({
            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) => { // ao retornar erro, avisa que ocorreu erro
                alert(JSON.stringify(err));
            }
        });
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 im 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:

Code: Select all

$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)";

$db->setQuery($query);
$db->execute();
This way the user can change inputs on the front-end and when he clicks save, i call this ajax to save things into the tables i created. And this does the job. I dont 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. But i dont 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 doesnt 'catch' the AJAX json call. I dont want to use form type submit since i dont want to reload the page.

If u need any other information please just let me know, i can add u on discord or such if u would be so kind to help me. Im reaching the end of my timeline on this project and really cant work this out.
Thank you again!

 

Post Reply

Return to “Joomla! 3.x Coding”