krionz
krionz

Reputation: 447

variable scope in different files

I'm trying to use a variable that is declared in file f1, in another file that is required by the same file.

Code in f1:

<?php

class Index extends Controller
{
    function __construct()
    {   
        parent::__construct();
    }

    public function index( $err = "" )
    {
        $err = "teeste";

        $this->view->render('index');
    }
}
?>

the line $this->view->render('index'); is correct, I'm sure because the form is appearing correctly.

Code of the other file:

<form action='<?php echo URL . 'user';?>' method="POST">
<input type="text" name="login" maxlength="35"/>
<input type="password" name="pass" maxlength="35"/>
<input type="submit" value="Entrar">
</form>

<?php 
echo $err; 
?>

I saw a lot of examples of this in the web but this one is not working even being simple.

Upvotes: 0

Views: 85

Answers (3)

krionz
krionz

Reputation: 447

Thanks for all.

i solve through this way.

<?php

class Index extends Controller
{
    public $err;

    function __construct()
    {   
        parent::__construct();
    }

    public function index()
    {
        $this->view->error = "teeste";

        $this->view->render('index');
    }
}
?>

and

<form action='<?php echo URL . 'user';?>' method="POST">
<input type="text" name="login" maxlength="35"/>
<input type="password" name="pass" maxlength="35"/>
<input type="submit" value="Entrar">
</form>

<?php 
echo $this->error; 
?>

Upvotes: 1

Mario Segura
Mario Segura

Reputation: 325

This looks a lot like codeigniter so I'm going to answer this how I see it

class Index extends Controller
{
    function __construct()
{   
    parent::__construct();
}

public function index( $err = "" )
{
    $data['err'] = "testee";

    $this->view->render('index', $data);
}
}
?>

and second file

<form action='<?php echo URL . 'user';?>' method="POST">
<input type="text" name="login" maxlength="35"/>
<input type="password" name="pass" maxlength="35"/>
<input type="submit" value="Entrar">
</form>

<?php 
echo $err; 
?>

Upvotes: 0

Barmar
Barmar

Reputation: 780724

You need to use a global variable. In the function:

public function index($err = "") {
    global $global_err;

    $global_err = "teeste";
    $this->view->render('index');
}

Then in your other file:

echo $global_err;

Upvotes: 0

Related Questions