CodeIgniter:嵌套视图中的表单操作


CodeIgniter: form action in nested view

我对代码点火器和表单有一点问题。

我正在从事一个项目,其中安全性非常重要。我使用代码点火器和引导程序。我有主视图,其中包括导航栏和其他一些 html。在该主视图中,我有<div id="change">,我正在通过按钮组进行更改。使用这些按钮,我在控制器中调用函数,其中包含$this->load->view('pages/something', $data);当我按下其中一个按钮时,<div id="change">会发生变化,并且在其中显示一个视图并且链接不会更改,因此主视图保持原样。但是,当按下其中一个按钮时,将显示带有窗体的视图。在其中我使用form_open('controller/function', $attributes);.问题是,当我提交此表单时,链接已更改,主视图不再显示在屏幕上。

我怎样才能实现,当我提交表单时,所有将要更改的只是<div id="change">而不是整个网站。

我知道我的英语不好,对不起。我感谢任何帮助。谢谢!

您可以使用 AJAX 提交表单,以下是使用 jQuery 的基本示例:

// this is the id of the form
$("#idForm").submit(function() {
    var url = "path/to/your/script"; // the script where you handle the form input.
    $.ajax({
        type: "POST",
        url: url,
        data: $("#idForm").serialize(), // serializes the form's elements.
        success: function(data) {
             // 'data' is the response from the php script.
             // Update <div id="change"> here.
        }
    });
    return false; // avoid to execute the actual submit of the form. (This stops the URL changing).
});

从这个答案修改的例子。

您想刷新页面的一部分而不是整个页面?也许你需要 AJAX。

另外,要使链接不更改,您必须更改

config/routes.php

喜欢这个:

$routes['controller/method'] = "your link";

读这个 .

希望对你有帮助