Dears
My component was a bit old. So I started to do it again on joomla 4. I have been looking for a few days, how to update or create data items by using Ajax in joomla 4. i found a lot of feedback on the Ajax interface J4. But I like to understand and therefore do by myself.
Normally, on FullCalendar, I have an event listener that triggers an Ajax call. Either it creates an object in the database or modifies it.
default.php :
$.ajax({
type: "post",
url: "index.php?option=com_litsupp&task=reservations.update",
data:
{
id: id,
arrivee: arrivee,
depart: depart,
lit: lit
},
cache: false,
success: function(data){
alert(data);
location.reload();
}
});
My reservationsController.php :
class ReservationsController extends FormController
{
public function update($key = null, $urlVar = null)
{
$this->checkToken();
$result = new stdClass;
$input = Factory::getApplication()->input;
$id = $this->input->getInt('id', 0);
$arrivee = $this->input->get('arrivee','','string');
$depart = $this->input->get('depart','','string');
$lit = $this->input->get('lit',0,'int');
$r = false;
//PR: Added the model instantiation
$model = $this->getModel('Reservations', 'Site');
if ($id > 0) {
$r = $model->updateResas(array(
'id' => $id,
'arrivee' => $arrivee,
'depart' => $depart,
'lit' => $lit
));
$r = true;
}
$result->status = $r;
$result->row = $id;
echo json_encode($result);
exit;
}
}
and my reservationModel :
public function updateResas()
{
$db = $this::getDbo();
$query = $db->getQuery(true);
$updateNulls = true;
// Create an object for the record we are going to update.
$object = new stdClass();
// Must be a valid primary key value.
$object->id = $id;
$object->arrivee = $arrivee;
$object->depart = $depart;
$object->lit = $lit;
// Update their details in the users table using id as the primary key.
// You should provide forth parameter with value TRUE, if you would like to store the NULL values.
$result = $this::getDbo()->updateObject('#__litsupp_reservations', $object, 'id', $updateNulls); }
I absolutely do not manage joomla 4. The last update of my component was on version 3.1 of joomla. Version 4 is still sorely lacking in documentation, if any of you can help me understand what I'm doing wrong. That would be great!
Thank you for your help
success: function(data){ alert(data);
displays my page the index.php codedie('Entered Update Function
before the$this->checkToken()
line to see that your AJAX call is actually making it as far as executing the update() function. This will help determine if the problem is getting to the update() function or within the update() function.XHR loading complete: POST "http://localhost/toolbox/index.php?option=com_litsupp&task=reservations.update".