【问题标题】:Call to undefined method CI_Loader::section() in codeigniter在 codeigniter 中调用未定义的方法 CI_Loader::section()
【发布时间】:2021-11-25 09:39:33
【问题描述】:

目前我正在尝试在codeigniter 的views 文件夹下编写一个AJAX 脚本。为此,我使用<?= $this->section('scripts')?> 在 php 文件中创建了一个脚本。但这样做会给我致命错误:调用未定义的方法 CI_Loader::section()。这是我的完整代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>CodeIgniter MVC Basics</title>
</head>
<body>
    <form method='post' action="index">
    <h1>Customer Details</h1>

    <input type="date" name="startDate"/>
    <input type="date" name="endDate"/>
    <select name="status">
        <option>Draft</option>
        <option>Unpublish</option>
        <option>Let</option>
    </select>
    <br/><br/>
    <button type="button" class="alertbox" name="alertbox">alertbox</button>    

    </form>
</body>
</html>

<?= $this->section('scripts')?>

<script>
    $(document).ready(function(){
        $(document).on('click', '.alertbox', function(){
            alert("Hello world");
        });
    });
</script>
<?= $this->endsection()?>

我想要它,以便每当单击按钮时,屏幕上都会弹出一个警报。

编辑我正在从我的控制器加载视图,如下所示:

$this->load->view('crm/test');

以下是错误:

【问题讨论】:

  • 你如何加载视图,我的意思是控制器是什么样的?
  • 我编辑了您的问题并添加了相关代码 - 您的问题需要包含所有相关信息,此处,而不是分散在其他问题之间。

标签: php ajax codeigniter


【解决方案1】:

这段代码:

$this->load->view('crm/test');

你就是load a view in Codeigniter 3

这段代码:

<?= $this->section('scripts')?>
...
<?= $this->endsection()?>

正在使用layouts, from Codeigniter 4。所以看起来你使用的是 CI4,但是作为shown on that layouts page,和the main views documentation,在 CI4 中渲染和显示视图的方式是不同的:

echo view('crm/test');

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
相关资源
最近更新 更多