CodeIgniter Forums
passing variable - shows invalid variable ... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: passing variable - shows invalid variable ... (/showthread.php?tid=92563)



passing variable - shows invalid variable ... - arcmax98 - 03-07-2025

this should have been easy.... but for some reason its not working ...
Controller..
Code:
<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\UserModel;

class Login extends BaseController
{
    public function index()
    {
        return view('login');
    }
 
    public function authenticate()
    {
        $session = session();
        $userModel = new UserModel();

        $email = $this->request->getVar('email');
        $password = $this->request->getVar('password');
       
        $user = $userModel->where('user_email', $email)->first();
       
        if(is_null($user)) {
            return redirect()->back()->withInput()->with('error', 'Invalid username or password.');
        }

        $pwd_verify = password_verify($password, $user['user_pass']);

        if(!$pwd_verify) {
            return redirect()->back()->withInput()->with('error', 'Invalid username or password.');
        }

        $ses_data = [
            'user_id' => $user['user_id'],
            'user_email' => $user['user_email'],
            'isLoggedIn' => TRUE
        ];

        $session->set($ses_data);
        $data = array("js_to_load"=> "hello");
     
       
        //$data['js_to_load']=array("https://cdn.datatables.net/2.2.2/js/dataTables.js","https://cdn.datatables.net/2.2.2/js/dataTables.tailwindcss.js");

        return view( '/dashboard', $data);
 
       
       
    }
}
View - dashboard
Code:
<?=$this->extend("layouts/postlogin_layout")?>
 
<?=$this->section("content")?>

        <?php echo $js_to_load; ?>
       
 

    <?=$this->include('partials/usersettings');?>
   
<?=$this->endSection()?>
keep getting
Quote:ErrorException - Undefined variable $js_to_load
I have tried everything ..
Code:
$this->load->view('/dashboard',$data);
return view( '/dashboard', $data);
echo view('dashboard', $data);
all give the same invalid variable
if in the view i put
Code:
print_r($_SESSION);
it print's the session...
I know i am making a very small mistake some where just dont know where ...


RE: passing variable - shows invalid variable ... - ozornick - 03-07-2025

Are you really getting to the place where the template is output? redirect() does not interrupt execution?
If so, do you need to set the default value: echo $js_to_load ?? 'Hello';


RE: passing variable - shows invalid variable ... - Fido L Dido - 03-08-2025

This is bizarre because I think the code looks fine. My best guess is that your dashboard view is getting returned somewhere else where $js_to_load isn't passed to it. Could it be something to do with the redirects, or perhaps another controller method is returning the dashboard view. That's where I'd start to look.

Alternatively you have unearthed a CI bug, but I think this seems unlikely. I might try renaming the variable to minimise this as a possibility.


RE: passing variable - shows invalid variable ... - demyr - 03-08-2025

Can you try print_r() in your controller?

PHP Code:
echo "<pre>";
print_r(your_js_message_data);
die(); 



RE: passing variable - shows invalid variable ... - InsiteFX - 03-09-2025

What version of CodeIgniter 4 are you running?

Another thing you can try is this:

PHP Code:
// Views/data.php

<?php


// Controller

public function index()
{
    $data = [
        'js_to_load' => 'Testing'
    ];

    return view('data'$data)
        view('your_view');



Did you try passing it like this?

PHP Code:
$data["js_to_load"] = "hello"



RE: passing variable - shows invalid variable ... - InsiteFX - 04-12-2025

Also I found that in the views with CodeIgniter 4.6.0 and PHP 8.4.5 I have to add the following in my views

PHP Code:
<?php
/**
 * Main View (menu_editor/index.php)
 * ----------------------------------------------
 *
 * @var TYPE_NAME $title
 * @var TYPE_NAME $menus
 */

$this->extend('layouts/main');
?>