Codeigntier/AJAX - 致命错误:调用未定义的函数 form_input() in


Codeigntier/AJAX - Fatal error: Call to undefined function form_input() in

我刚刚成功地让 AJAX 从数据库中获取数据,并将其呈现为在单击按钮时显示的div。但是我在读取视图中包含php的第一行时出现此错误。

这是我的视图文件 html

<div id="association-list-wrapper" class="list-wrapper">
        <?php echo form_input('filter', 'Filter by key, name, or location', array('class' => 'text-input filter', 'data-label-value' => 'Filter by key, name, or location')) ?>
        <section class="table-wrapper">
            <table id="association-list" class="filter-results">
                <tbody>
                    <?php foreach ($associations as $association): ?>
                    <tr id="<?php echo key($association)?>" class="result-row">
                        <td class="td col1"><p id=""><?php echo $association['AssociationKey'] ?></p></td>
                        <td class="td col2"><a href="<?php echo base_url('association/'.$association['AssociationKey']) ?>"><?php echo $association['Name'].' '.$association['Title'] ?></a></td>
                        <td class="td col3"><p><?php echo $association['City'].', '.$association['State'] ?></p></td>
                    </tr>
                    <?php endforeach ?>
                </tbody>
            </table>
        </section>
    </div>

这是我的JS:

$('li#page1').click(function(e){
    e.preventDefault();
    $('#pick-list').show();
    $('#association-pick-list').show();
    $.ajax({
        type: 'get',
        url: 'http://myapp.loc/association/pick-list',
        dataType: 'html',
        success: function (html) {
          $('#shadowbox').load('http://myapp.loc/application/views/association_management/pick_list.php');
        }
    });
    $('.shadowbox-wrapper').fadeIn(250);
    $('.shadowbox-window').fadeIn(250);
});

这是我的控制器:

public function pick_list()
{
    $this->load->helper('form');
    $data['associations'] = $this->association_model->get_all_associations();
    $this->load->view('association_management/pick_list', $data);
}

为什么它不会呈现页面?我测试了触发控制器/方法的 URL,它呈现得很好,但是当它使用 AJAX 渲染时,所有 php 都会中断。

因此

而发生错误

$('#shadowbox').load('http://myapp.loc/application/views/association_management/pick_list.php');

如果像上面一样直接加载视图,则无法在视图页面中访问来自控制器的数据。因此,我建议您从控制器获取数据并在您的视图上渲染。

您的控制器

public function pick_list()
{
    $this->load->helper('form');
    $associations = $this->association_model->get_all_associations();
    echo json_encode(['data'=>$associations]);
}

现在使用返回的数据呈现视图。

试试这个,如果你没有得到,请问,我会帮助你

在视图页面中使用它而不是 form_input((

echo form_open('filter',  array('class' => 'text-input filter', 'data-label-value' => 'Filter by key, name, or location'));

在控制器中,也添加这个

$this->load->library('form_validation');

以及视图表单中的一个错误关联是从控制器传递的数组名称。但你已经完成了$association。使用$association

为您的表单提供 id 并在 ajax 中使用该 id在视野中

 echo form_open('filter',  array('class' => 'text-input filter', 'data-label-value' => 'Filter by key, name, or location','id'=>'myform'));

这个网址是对的吗?

网址: 'http://myapp.loc/association/pick-list',

还是像这样?

网址:'http://myapp.loc/association/pick_list',