Welcome Guest, Not a member yet? Register   Sign In
passing variable - shows invalid variable ...
#1

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 ...
Reply
#2

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';
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 03-08-2025, 03:55 AM by Fido L Dido.)

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.
Reply
#4

Can you try print_r() in your controller?

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

(This post was last modified: 03-09-2025, 09:33 PM by InsiteFX. Edit Reason: Updated content )

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"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

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');
?>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB