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!