如何在codeigniter控制器中添加javascript代码__construct(){}

时间:2018-06-14 05:06:31

标签: php codeigniter codeigniter-3

我想添加codeigniter controller __construct()来在localStorage中存储用户数据,

这是我的下面代码

<?php
class Dashboard extends CI_Controller
{
    public function __construct() {
    ?>
    <script type="text/javascript">
        localStorage.setItem('ruuser_id', '<?php echo $_SESSION['ruuser_id'];?>');
        localStorage.setItem('rufname', '<?php echo $_SESSION['rufname'];?>');
        localStorage.setItem('rulname', '<?php echo $_SESSION['rulname'];?>');
        localStorage.setItem('bru_staff', '<?php echo $_SESSION['bru_staff'];?>');
        localStorage.setItem('ruuser_emailid', '<?php echo $_SESSION['ruuser_emailid'];?>');
        localStorage.setItem('ruseller_id', '<?php echo $_SESSION['ruseller_id'];?>');
    </script>
    <?php
    parent::__construct();
}

当我添加脚本代码时,我收到以下错误:

Severity: Warning

Message: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/deveventadviser/public_html/development/application/controllers/Dashboard.php:7)

Filename: Session/Session.php

您能否建议如何添加__construct函数

1 个答案:

答案 0 :(得分:0)

这不是在php类文件中使用javascript代码的正确方法,但解决方案就是这样。你应该定义一个属性变量并像这样使用它。

<?php 

class Dashboard extends CI_Controller

{
 public $this->jScript = ''; 
public function __construct() 

{

    parent::__construct();

    // set property value 

    $this->jScript = "<script type='text/javascript'> //your stuff here </script>";



}

public function abc() {

$data['jScript'] = $this->jScript;

$this->load->view('abc',$data);

} 

}