0

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

4
  • Are you able to confirm if the update() function is being entered by the AJAX call or not?
    – Irata
    Aug 12 at 9:21
  • Currently no, my controller does not return anything and success: function(data){ alert(data); displays my page the index.php code Aug 12 at 11:04
  • What I mean is if you use xdebug and put a breakpoint at the first line of update() or insert a die('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.
    – Irata
    Aug 12 at 12:01
  • Sorry for the response time. After the test I have: XHR loading complete: POST "http://localhost/toolbox/index.php?option=com_litsupp&task=reservations.update". Aug 12 at 17:54

1 Answer 1

1

Dears & dear Irata

I finally changed my way. This is the way of updating that blocked on the model side. Here are the changes if it can help

Default.php

    $.ajax({
        type: "post",
        url: "index.php?option=com_litsupp&task=reservations.update",
        data:
        {
            token : csrf,
            id : id,
            arrivee : arrivee,
            depart : depart,
            lit : lit
        },
        cache: false,
        success: function(){
            location.reload();

reservationsController.php

public function update($key = null, $urlVar = null)
{
    $result = new \stdClass();
    $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');
    $model = $this->getModel('Reservations', 'Site');
    if ($id > 0) {
        $return = $model->updateResas(array(
            'id' => $id,
            'arrivee' => $arrivee,
            'depart' => $depart,
            'lit' => $lit
        ));
    }
    $result->status = $r;
    $result->row = $id;
    echo json_encode($result);
}

reservationsModel :

public function updateResas()
{
    $db = $this->getDbo();
    $query = $db->getQuery(true);
    $query
        ->update($db->quoteName('#__litsupp_reservations'))
        ->set($db->quoteName('arrivee') . ' = "' . $_POST['arrivee'] . '"')
        ->set($db->quoteName('depart') . ' = "' . $_POST['depart'] . '"')
        ->set($db->quoteName('lit') . ' = "' . $_POST['lit'] . '"')
        ->where($db->quoteName('id') . ' = "' . $_POST['id'] . '"');
    $db->setQuery($query);
    $result = $db->execute();
}

Thanks a lot Irata for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.