EDITOR DATATABLE AJAX POST IS EMPTY ARRAY ON CI4

EDITOR DATATABLE AJAX POST IS EMPTY ARRAY ON CI4

chamssoudinechamssoudine Posts: 8Questions: 1Answers: 0

Hello,

I need to experiment Editor Datatable on CI4, first attempt everything was working and then all crached. I spend two days trying to found the issue but always failed. if someone can help fix it.

Credentials :

https://comebillets.com/agenceamani.com/login
Login : admin@example.com
Pass: admin

Editor link : https://comebillets.com/agenceamani.com/loyers

DataTables warning: table id=loyersTable - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1:
Status code 200 OK but $_POST variable is empty on the CI4 controller

Replies

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    but $_POST variable is empty on the CI4 controller

    This i your Datatables ajax config:

            ajax: {
                url: "https://comebillets.com/agenceamani.com/Auth/LoyersController/ViewLoyers",
                type: "POST"
            },
    

    Are you expecting POST parameters to be sent? If so then use ajax.data.

    This is the JSON response:

    Array ( )

    I'm not familiar with Codeigniter but maybe this SO thread will give you some ideas of how to return the JSON response. Debugging why the array is empty would require debugging the server script. There is nothing we can look at on the client side to help with this issue.

    Kevin

  • chamssoudinechamssoudine Posts: 8Questions: 1Answers: 0
    edited April 22

    Hello Kevin,

    My Controller is function :

    public function ViewLoyers()
        {
            try {
                if (! $this->session->isLoggedIn) {
                    return redirect()->to('login');
                }
                $editor = new EditorLib();
                print_r($_POST);
                $result = $editor->loyers($_POST);
    
                // Handle the result if needed
            } catch (\Exception $e) {
                // Log or handle the exception
                echo 'Caught exception: ',  $e->getMessage(), "\n";
            }
        }
    

    $_POST variable is empty, is not supposed to post the table columns ? to model? im confused.

    //Model

    <?php
    
    namespace App\Models;
    //require_once APPPATH . 'Libraries/EditorLib/DataTables.php';
    
    use CodeIgniter\Model;
    use DataTables\Editor;
    use DataTables\Editor\Field;
    use DataTables\Editor\Format;
    use DataTables\Editor\Join;
    use DataTables\Editor\Upload;
    use DataTables\Editor\Validate;
    use DataTables\Editor\ValidateOptions;
    
    class LoyersModel extends Model
    {
        protected $table = 'properties';
        protected $primaryKey = 'id';
        protected $allowedFields = ['Title', 'Description', 'Price', 'Location', 'Type', 'Status'];
        
        private $editorDb = null;
         
        public function init($editorDb)
        {
            $this->editorDb = $editorDb;
        }
    
        public function getLoyers($post)
        {
            try {
                // Initialize the Editor instance with the database connection and table name
                $editor = new Editor($this->editorDb, 'properties');
                $editor->leftJoin('properties_details', 'properties.id', '=', 'properties_details.propId');
    
                // Define fields with validators
                $editor->fields(
                    Field::inst('properties.Title')->validator('Validate::notEmpty'),
                    Field::inst('properties.Description')->validator('Validate::notEmpty'),
                    Field::inst('properties.Price'),
                    Field::inst('properties.Location')->validator('Validate::notEmpty'),
                    Field::inst('properties.Type')->validator('Validate::notEmpty'),
                    Field::inst('properties.Status')->validator('Validate::notEmpty'),
                    Field::inst('properties_details.NumBedrooms'),
                    Field::inst('properties_details.NumBathrooms'),
                    Field::inst('properties_details.Area'),
                    Field::inst('properties_details.ParkingSpaces'),
                    Field::inst('properties_details.HasGarden'),
                    Field::inst('properties_details.HasGarage')
                );
    
                // Enable debugging (optional)
                $editor->debug(true);
    
                // Process the request and return JSON response
                $result = $editor->process($post)->json();
    
                return $result;
            } catch (\Exception $e) {
                // Handle any exceptions or errors
                return ['error' => $e->getMessage()];
            }
        }
    
    
    } 
    

    Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide

  • chamssoudinechamssoudine Posts: 8Questions: 1Answers: 0

    Noted that same code is working on CI3

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    $_POST variable is empty, is not supposed to post the table columns ? to model? im confused.

    Only when serverSide is enabled will the server side processing parameters be sent to the server. Are the server side processing parameters what you are expecting to be sent?

    Kevin

  • chamssoudinechamssoudine Posts: 8Questions: 1Answers: 0
    edited April 23

    Js code look like :

    const editor = new DataTable.Editor({
            ajax: '<?php echo site_url("Auth/LoyersController/ViewLoyers")  ?>',
            fields: [
                {
                    label: "Titre:",
                    name: "properties.Title",
                },
                {
                    label: "Description:",
                    name: "properties.Description",
                    type: "textarea",
                },
                {
                    label: "Prix:",
                    name: "properties.Price",
                },
                {
                    label: "Location:",
                    name: "properties.Location",
                },
    
     const table = new DataTable("#loyersTable", {
            ajax: {
                url: "<?php echo site_url() ?>Auth/LoyersController/ViewLoyers",
                type: "POST",
            },
            buttons: [
                { extend: "create", editor },
                { extend: "edit", editor },
                { extend: "remove", editor },
            ],
            dom: "lQBfrtip",
            order: [1, "asc"],
            responsive: true,
            //scrollX: true,
            select: {
                style: "os",
                selector: "td:first-child",
            },
            columns: [
                {
                    data: null,
                    defaultContent: "",
                    className: "select-checkbox",
                    orderable: false,
                },
                { data: "properties.Title" },
                { data: "properties.Description" },
                { data: "properties.Price" },
                { data: "properties.Location" },
                { data: "properties.Type" },
                { data: "properties.Status" },
                { data: "properties_details.NumBedrooms" },
                { data: "properties_details.NumBathrooms" },
                { data: "properties_details.Area" },
                { data: "properties_details.ParkingSpaces" },
                { data: "properties_details.HasGarden" },
                { data: "properties_details.HasGarage" },
            ],
    

    Edited by Allan to add syntax highlighting

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    With that initialisation, a POST request, with no data parameters will be sent to ViewLoyers when the DataTable is initialised.

    For create, edit and delete actions, Editor will POST data in the format shown here to the same route.

    At the moment ViewLoywers is returning:

    Array
    (
    )
    

    which looks like a PHP print_r() statement. That isn't valid JSON, hence the error.

    What I would suggest doing is adding some logging to make sure that you are getting into the getLoyers method, and where in it you are getting to.

    Allan

  • chamssoudinechamssoudine Posts: 8Questions: 1Answers: 0

    Hello Allan,

    Thank you, the issue was here : DB code was deleted by error.

    $this->loyersModel->init($db);

    public function loyers($post)
    {
    // DataTables PHP library
    require_once APPPATH . 'Libraries/EditorLib/DataTables.php';
    $this->loyersModel->init($db);
    return $this->loyersModel->getLoyers($post);
    }

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    No worries - good to hear you got it working.

    Allan

Sign In or Register to comment.