CodeIgniter Forums
Ajax post failing with TypeError - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Ajax post failing with TypeError (/showthread.php?tid=92885)



Ajax post failing with TypeError - PaulC - 05-10-2025

I have defined the route and when I actually fire the JS I end up with the following: (CI 4.6.0, on FireFox). Is this a CI problem please?
{
    "title": "TypeError",
    "type": "TypeError",
    "code": 500,
    "message": "CodeIgniter\\HTTP\\Request::fetchGlobal(): Argument #3 ($filter) must be of type ?int, true given, called in /var/www/html/gnb/ci460/system/HTTP/IncomingRequest.php on line 714",
    "file": "/var/www/html/gnb/ci460/system/HTTP/RequestTrait.php",
    "line": 258,
    "trace": [
        {
            "file": "/var/www/html/gnb/ci460/system/HTTP/IncomingRequest.php",
            "line": 714,
            "function": "fetchGlobal",
            "class": "CodeIgniter\\HTTP\\Request",
            "type": "->"
        },
        {
            "file": "/var/www/html/gnb/ci460/app/Controllers/TheBigPicture.php",
            "line": 148,
            "function": "getPost",
            "class": "CodeIgniter\\HTTP\\IncomingRequest",
            "type": "->"
        },
        {
            "file": "/var/www/html/gnb/ci460/system/CodeIgniter.php",
            "line": 933,
            "function": "interact",
            "class": "App\\Controllers\\TheBigPicture",
            "type": "->"
        },
        {
            "file": "/var/www/html/gnb/ci460/system/CodeIgniter.php",
            "line": 507,
            "function": "runController",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->"
        },
        {
            "file": "/var/www/html/gnb/ci460/system/CodeIgniter.php",
            "line": 354,
            "function": "handleRequest",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->"
        },
        {
            "file": "/var/www/html/gnb/ci460/system/Boot.php",
            "line": 334,
            "function": "run",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->"
        },
        {
            "file": "/var/www/html/gnb/ci460/system/Boot.php",
            "line": 67,
            "function": "runCodeIgniter",
            "class": "CodeIgniter\\Boot",
            "type": "::"
        },
        {
            "file": "/var/www/html/gnb/ci460/public/index.php",
            "line": 56,
            "function": "bootWeb",
            "class": "CodeIgniter\\Boot",
            "type": "::"
        }
    ]
}


RE: Ajax post failing with TypeError - paulbalandan - 05-10-2025

Can you share the code of TheBigPicture controller?


RE: Ajax post failing with TypeError - PaulC - 05-10-2025

(2 hours ago)paulbalandan Wrote: Can you share the code of TheBigPicture controller?

PHP Code:
<?php

namespace App\Controllers;

use 
App\Controllers\BaseController;
use 
App\Models\The_big_picture\GenerateModel;

class 
TheBigPicture extends BaseController
{
    // temp limit on biggest viewable tile
    public $find_limit 1000;

    //-------------------------------------------------------------
    public function index($level2 ''$level3 ''$find '')
    {

        //Get data values for the big picture
        $data['starting_img'] = '/images/the-big-picture/main.png';
        $data['starting_level'] = 1;
        $data['starting_level2'] = '';
        $data['starting_level3'] = '';
        $data['tab_text'] = 'Start <small>(Click anywhere in The BIG Picture to zoom in)</small>';

/*
        $data['count_of_ikulpics'] = $this->IkulpicModel->count_ikulpics();
        $data['count_of_countries'] = $this->IkulpicModel->count_countries();
        $data['find_limit'] = $this->find_limit;

        $latest_ikulpics = $this->IkulpicModel->get_latest_ikulpics(15);
        if ($latest_ikulpics) {
            foreach ($latest_ikulpics as $q => $ll) {
                $insp = $this->IkulpicModel->get_inspiration($ll->id);
                $latest_ikulpics[$q]->imgsrc = $insp;
            }
        }
        $data['latest_ikulpics'] = $latest_ikulpics;
*/

        if ($level3) {
            //Validate
            if (!is_valid_level($level3)) {
                return redirect()->to('the-big-picture');
            }

            $data['starting_img'] = '/images/the-big-picture/level3/segment'.$level2.'/'.$level3.'.png';
            $data['starting_level'] = 3;
            $data['starting_level2'] = $level2;
            $data['starting_level3'] = $level3;
            //$data['tab_text'] = $this->IkulpicModel->get_tab_text($level2, $level3);

        //setMessage('info','This is only part of the whole Smile Project!');
        } elseif ($level2) {
            if (!is_valid_level($level2)) {
                return redirect()->to('the-big-picture');
            }

            $data['starting_img'] = '/images/the-big-picture/level2/'.$level2.'.png';
            $data['starting_level'] = 2;
            $data['starting_level2'] = $level2;
            //$data['tab_text'] = $this->IkulpicModel->get_tab_text($level2);
        }

        //If find
        if ($find == 'tbd,tbd') {
            // this causes the modal to be displayed
            $data['view_ikulpic_onload'] = array(
                                                  'col' => 'tbd',
                                                  'row' => 'tbd'
                                                );

} elseif (
$find) {
            //Validate
            if (!is_valid_reference($find)) {
                //Invalid pixel
                setNotice('invalid ikulpic');
                return redirect()->to('the-big-picture');
            }

            list($col$row) = explode(','$find2);

            //Calculate grid reference
            $find_level2_row ceil($row 200);
            $find_level2_col ceil($col 200);
            $find_level2 $find_level2_col.'_'.$find_level2_row;

            $find_level3_row ceil(($row - ((ceil($row 200) - 1) * 200)) / 40);
            $find_level3_col ceil(($col - ((ceil($col 200) - 1) * 200)) / 40);
            $find_level3 $find_level3_col.'_'.$find_level3_row;


            if ($level2 != $find_level2 || $find_level3 != $level3) {
                return redirect()->to('the-big-picture/find-ikulpic/'.$col.','.$row);
            }

            // this causes the modal to be displayed
            $data['view_ikulpic_onload'] = array(
                                                  'col' => $col,
                                                  'row' => $row,
                                                );
        }

        //Set data
        $this->template->bodyData $data;
        $this->template->footerData = array('show_big_picture_control' => true);

        //Set JS
        $this->template->loadJS('big_picture');

        $this->template->render('the-big-picture/the_big_picture');
    }

    //-------------------------------------------------------------
    //Find Ikulpic
    public function find_ikulpic($col$row)
    {
        //Check row and col
        if (!is_valid_reference($col.','.$row)) {
            //Invalid pixel
            setNotice('invalid ikulpic');
            return redirect()->to('the-big-picture');
        } elseif ($row $this->find_limit || $col$this->find_limit) {
setMessage('danger','Please choose another goBIG (current max: '.$this->find_limit.')');
return 
redirect()->to($this->get->gnhome());
} else {
        //Calculate grid reference
        $level2 ceil($col 200).'-'.ceil($row 200);
        $level3 ceil(($col - ((ceil($col 200) - 1) * 200)) / 40).'-'.ceil(($row - ((ceil($row 200) - 1) * 200)) / 40);

        //Go to URL
        return redirect()->to('the-big-picture/'.$level2.'/'.$level3.'/'.$col.','.$row);
}
    }

    //-------------------------------------------------------------
    //Interact with big picture (AJAX CALL)
    public function interact($task)
    {
        //Task
        $task = (string) clean($task);

        //Run interact tasks
        switch ($task) {
        // Get an Ikulpic
            case 'get_ikulpic':
            {
                //Post reference
                $reference $this->request->getPost('reference'true);

                //Validate
                if ($reference != 'tbd,tbd' && !is_valid_reference($reference)) {
                    //Invalid ikulpic
                    //Get error message
                    $error $this->get->error_text('invalid ikulpic');

                    //Output
                    echo json_encode(array('success' => 0'error_message' => $error));
                    exit;
                }

                //Output result from modal
                $this->IkulpicModel->get_for_modal($reference);
            }
            break;

        //Favourite
            case 'toggle_favourite':
            {
                //Vars
                $favourite = (bool) $this->request->getPost('favourite'true) == 1;
                $reference = (string) $this->request->getPost('reference'true);
                $returnUri = (string) $this->request->getPost('returnUri'true);

                //Get ikulpic
                $ikulpic $this->get->ikulpic($reference);

                //Check logged in
                if (!$this->get->user()) {
                    $errmsg $this->get->error_text('login required');
                    // add return URI after signin
                    $errmsg str_replace('/sign-in''/sign-in/'.$returnUri$errmsg);
                    echo json_encode(array('success' => 0'error_message' => $errmsg));
                    exit;
                }

                //Is valid
                if (!$ikulpic || $ikulpic->status != IKUL_RENDERED) {
                    echo json_encode(array('success' => 0'error_message' => $this->get->error_text('unable to process your request')));
                    exit;
                }

                //Process
                //Check DB
$builder $this->db->table('favourites');
                $favourited $builder->getWhere(array('user_id' => $this->get->user('id'), 'ikulpic_id' => $ikulpic->id), 1)->getNumRows()();
$builder->freeResult();

                if ($favourite && !$favourited) {
                    //Add to DB
                    $insert['ikulpic_id'] = $ikulpic->id;
                    $insert['user_id'] = $this->get->user('id');
                    $insert['time'] = time();

                    $builder->insert($insert);
$builder->freeResult();

/*
                    //User activity
                    $user_id = $this->get->user('id');
                    $data = array(
                              'type' => 'ikulpic_favourited',
                              'ikulpic' => $ikulpic->reference,
                              'reference' => $ikulpic->reference
                              );

                    $this->activity_model->log($data, $user_id);

                    if ($user_id != $ikulpic->user_id) {
                        $data = array(
                                  'type' => 'my_ikulpic_favourited',
                                  'ikulpic' => $ikulpic->reference,
                                    'reference' => $ikulpic->reference,
                                  'user' => $user_id
                                  );

                        $this->activity_model->log($data, $ikulpic->user_id);
                    }
*/
                }
                //Unlike
                elseif (!$favourite && $favourited) {
                    $builder->delete(array('user_id' => $this->get->user('id'), 'ikulpic_id' => $ikulpic->id));
                }
                //Done
                echo json_encode(array('success' => 1));
                exit;
            }
            break;

        //Feature
            case 'toggle_featured':
            {
                //Vars
                $feature = (bool) $this->request->getPost('featured'true) == 1;
                $reference = (string) $this->request->getPost('reference'true);

                //Get ikulpic
                $ikulpic $this->get->ikulpic($reference);

                //Check logged in
                if (!$this->get->user()) {
                    echo json_encode(array('success' => 0'error_message' => $this->get->error_text('login required')));
                    exit;
                }

                //Is valid
                if (!$ikulpic || $ikulpic->status != IKUL_RENDERED || $ikulpic->user_id != $this->get->user('id')) {
                    echo json_encode(array('success' => 0'error_message' => $this->get->error_text('unable to process your request')));
                    exit;
                }

                //Process

                //Remove all others
$builder $this->db->table('ikulpics');
                $builder->update(array('featured' => 0'user_id' => $this->get->user('id')));
$builder->freeResult();

                //Make featured
                if ($feature) {
                    $builder->update('ikulpics', array('featured' => 1'id' => $ikulpic->id));
                }

                //Done
                echo json_encode(array('success' => 1));
                exit;
            }
            break;

        // Send Share Email
            case 'send_share_email':
            {
                $email_from 'From:'.$this->get->setting('Default from name').'<'.$this->get->setting('Default from email').'>';
                $email_to = (string) $this->request->getPost('eto'true);
                $content = (string) $this->request->getPost('econtent'true);
                $reference = (string) $this->request->getPost('eref'true);
                $subject $this->get->setting('Share email subject line');
                $reference base_url().'the-big-picture/find/'.$reference;
                $content .= "\n".'The iKulpic is at: <a href="'.$reference.'">'.$reference.'</a>';

                if (!mail($email_to$subject$content$email_from)) {
                    echo json_encode(array('fail' => 1));
                } else {
                    echo json_encode(array(
                    'success' => 1,
                    'email_to' => $email_to,
                    'email_su' => $subject,
                    'email_co' => $content,
                    'email_fr' => $email_from,
                    ));
                }
                exit;
            }
            break;

        //Unknown
            default:
            {
                //Output
                echo json_encode(array('success' => 0));
                exit;
            }
        // end switch
    }

    //-------------------------------------------------------------
    //Redraw big picture
    public function redraw_big_picture($clear false)
    {
        $this->GenerateModel->redraw_the_big_picture($clear);
    }

    //-------------------------------------------------------------
    public function draw_area($modal_ref$ikborder)
    {
        // get db object of {username,color} for x+-border,y+-border
        // same function will be used to update ajax calls
        $this->template->bodyData['data'] = $this->IkulpicModel->get_area_data($modal_ref$ikborder);
        $this->template->bodyData['ikborder'] = $ikborder;
        $this->template->bodyData['mref'] = $modal_ref;
        $this->template->loadJS(array('bp_area'));

        // render the view
        $this->template->render('the-big-picture/area_view');
    }

    //-------------------------------------------------------------
    public function find_free()
    {
        // get closest free ikulpic for join in buttons
        $ref $this->IkulpicModel->find_free(); //returns x,y
        // pass to find_ikulpic
        list($col$row) = explode(','$ref);
        $this->find_ikulpic($col$row);
    }