【问题标题】:CodeIgniter - Two Forms, One PageCodeIgniter - 两种形式,一页
【发布时间】:2013-05-29 18:26:00
【问题描述】:

我在理解 MVC 的概念和一次显示多个表单方面遇到了一个基本问题。我尝试了多种方法,但仍然卡住 - 那是因为我认为我没有正确理解 CI 和 MVC。

我尝试对两种不同的表单使用两种不同的视图。没用。我尝试在我的控制器中为每个表单使用一个功能。那也没有用。我不知道该怎么办。

我应该这样做吗?

  1. 创建一个控制器并在其中包含一个 index() 函数。
  2. 为这个 index() 中的每个表单构建我的表单元素
  3. 创建 1 个显示两个表单的视图并从 index() 中调用它
  4. 使用 form_open 将提交操作定向到另一个函数 - 调用它 validate()
  5. 验证传入的所有内容,发回错误
  6. 不知何故,这是我不明白的主要内容,如果表格已正确填写,请完成一项操作。

6 是我最大的问题。不知道该怎么做。例如,在成功完成表单后,我希望我的用户在所选位置创建目录 - 所以我使用 mkdir() - 所以我需要在 validate() 函数中使用 if 语句还是什么? /p>

更新

这是我目前创建的代码;

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// 形成 CodeIgniter 控制器 管理员类扩展 CI_Controller {

// Controller constructor
public function __construct()
{
    parent::__construct();
    // Load form helper required to validate forms
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');        
}

//*************************************************//

// Prepare data for the view to output the forms
public function index()
{

    //*****************************************************//
    //returns a drop down list of radio buttons, one for each directory
    $map_one = $this->recursive_model->iterate_add_folder_names();
    $data['folder_list_add'] = $map_one;    
    //****************************************************//
    //*****************************************************//
    //also returns a drop down list of radio buttons (slightly different), one for each directory
    $map_two = $this->recursive_model->iterate_folder_names();
    $data['folder_list_select'] = $map_two; 
    //****************************************************//

    //load the views and the forms
    $this->load->view('templates/header.php');
    $this->load->view('admin/add_new_folder.php', $data);
    $this->load->view('admin/add_new_file.php', $data);
    $this->load->view('templates/small_footer.php');
}

//*************************************************//

//function if adding a new directory to the current structure
public function add_folder()
{
    //need to select a directory for it to go under
    $this->form_validation->set_rules('new_folder', 'New Folder', 'required');
    //and name the new directory
    $this->form_validation->set_rules('new_folder_name', 'New Folder Name', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->index();
    }
    else
    {   
        if($this->input->post())
        {
            $new_folder = $this->input->post('new_folder');
            $new_folder_name = $this->input->post('new_folder_name');
            $folder_path = "/var/www/html/mike/content".$new_folder."/".$new_folder_name;
            mkdir($folder_path, 0777);
            $this->index();
        }
    }

}

//*************************************************//

public function add_file()
{

    //folder location and name of file
    $folder_name = $this->input->post('folder_name');
    $new_folder_name = $this->input->post('file_name');

    //validation rules
    $this->form_validation->set_rules('folder_name', 'Folder Name', 'required');
    $this->form_validation->set_rules('file_name', 'File Name', 'required');

    //if there is an error with validation
    if ($this->form_validation->run() === FALSE)
    {
        //gets stuck here every time when trying to upload a new folder :(
        $this->index();
    }
    //if there is not an error with validation
    else
    {   
        //$folder_name will be something like "http://www.example.com/publications/people/reports"
        $config['upload_path'] = $folder_name;
        $config['allowed_types'] = 'gif|jpg|png|html|pdf|xls';
        $this->load->library('upload', $config);

        //if file cannot be loaded (due to $config perhaps?)
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            $this->index();
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->index();
        }

    }   

}

//*************************************************//

}

这是一个视图(add_new_file.php);

<div id="container">

<h1>Upload A File/Publication</h1>

<div id="body">

<?php //echo $error;?>

<?php echo form_open_multipart('admin/add_file');?>

<?php echo $folder_list_select; ?>   &nbsp;&nbsp; 
<input type="file" name="file_name" size="20" />   &nbsp;&nbsp; 
<input type="submit" value="upload" />

</form>

</div>

这是另一个(add_new_folder.php)

div id="container">

<h1>Add A New Folder</h1>

<div id="body">

<?php echo validation_errors(); ?>

<?php echo form_open('admin/add_folder');?>

<?php echo $folder_list_add; ?>   &nbsp;&nbsp; 
New Folder Name: <input type="text" name="new_folder_name">   &nbsp;&nbsp; 
<input type="submit" value="upload" />

</form>

</div>

我希望这有助于回答这个问题。

基本上,我可以让第一部分工作 - 添加文件夹 - 但我无法让添加文件工作。这是因为 if ($this->form_validation->run() === FALSE) 总是返回 false。我认为它可能正在查看其他表单中的表单元素 - 它不应该这样做。我错过了什么?

【问题讨论】:

  • 当你说它不起作用时 - 以什么方式?表格是否显示不正确,是否有错误,或者只是没有达到预期的效果?您可以随心所欲地加载视图 - 一个视图、多个视图等。您是对的,在您的控制器中,您可能需要一个函数用于每个表单的操作。对于您最大的问题,我建议您查看the documentation for CodeIgniter's form validation library - 它解释了如何处理好表单的过程。
  • 您提到了 2 个表格。我假设您已经使用了 1 个表单,并且您正在尝试制作 2 个表单。请注意,您不能嵌套表格。记得关闭你的表单标签。

标签: forms codeigniter controller


【解决方案1】:

我应该这样做吗;
1.创建一个控制器并在其中包含一个 index() 函数。
[为了便于交流,我们调用这个控制器 Users 谢谢 -编辑]

当然。这很酷。您还可以在该控制器中拥有一个名为 editbanana 或其他名称的函数;无论哪种方式都有效。仅使用index 方法(函数),url 可能看起来像http://example.com/index.php/users,而如果你向控制器添加另一个方法,比如banana,url 可能看起来像http://example.com/index.php/users/banana

2 。为这个 index() 中的每个表单构建我的表单元素

嗯,通常不会在控制器中创建表单元素。这就是 MVC 中的 V 出现的地方——您查看的内容进入 view

所以,一个人可能会做类似的事情

// Users Controller
class Users extends CI_Controller{
    function index(){
        //index method
    }

    function banana(){
        $this->load->view('banana_view');
    }
}

然后在application/views/banana_view.php 中创建表单。当您访问http://example.com/users/banana 时,您将看到您在banana_view.php 中创建的表单。

3 .创建 1 个显示两种表单的视图并从 index() 中调用它

当然,这样就可以了。但请记住,每个&lt;form&gt;&lt;/form&gt; 内部都需要自己的&lt;input type="submit" name="Lets GO"&gt;,因此需要在某个地方发送每个表单数据。这是action=""。您可以将其省略,但请注意,它会将表单发送到您当前所在的任何页面(在我们的示例中为http://example.com/index.php/users/banana),因此您必须在banana() 方法中处理表单数据.但是,通常,它将通过form_open() 设置。像form_open('index.php/users/eat_banana'); 这样的东西会生成&lt;form action="index.php/users/eat_banana"...

4 。使用 form_open 将提交操作定向到另一个函数 - 调用它 validate()

别叫它late_for_dinner。但说真的,validate 有点宽泛——验证什么?验证为什么?至于验证https://www.codeigniter.com/user_guide/libraries/form_validation.html。但是,在您了解 CodeIgniter 的基础知识之后,您应该跨过那座桥(不会花很长时间)。

5 .验证所有进来的东西,发回错误

查看最后一个问题。

6 .不知何故,这是我不明白的主要部分,如果表格已正确填写,请完成一个操作。

很多时候人们会显示成功信息

class Users extends CI_Controller{

    function index(){
        //index method
    }

    function banana(){
        $this->load->view('banana_view');
    }

    // assuming form_open('index.php/users/eat_banana'); in banana_view
    function eat_banana(){
        //make sure that this is a POST
        if($this->input->post()){
            // do things with the data
            // typically it gets saved to a database
            // via a model (the M in MVC)
            // http://ellislab.com/codeigniter/user-guide/general/models.html

            if($saved_to_db){
                // set message to send to the view
                $data['message'] = "Everything went OK";
            }else{
                $data['message'] = "but who was database? data didn't save :(";
            }
            // load the view and send the data
            $this->load->view('eat_banana', $data);
        }
     }

application/views/eat_banana.php:

 <!DOCTYPE html>
 <html>
 <head></head>
 <body>
 <div>
     <b>Form submitted.</b><br />
     The message is: <?php echo $message; ?>
 </div>
 </html>

其他时候,人们可能更喜欢重定向

class Users extends CI_Controller{

    function index(){
        //index method
    }

    function banana(){
        $this->load->view('banana_view');
    }

    // assuming form_open('index.php/users/eat_banana'); in banana_view
    function eat_banana(){
        //make sure that this is a POST
        if($this->input->post()){
            // do things with the data             
            if($saved_to_db){
                // just send them to the homepage
                redirect('/');
            }else{
                // send them back to the form
                redirect('index.php/users/banana');
            }
        }
     }

所以,

M 用于模型。模型用于与数据库对话。

V 用于Vend 视图。视图将文本、表单、图片、gif 等任何内容呈现到屏幕上。反正就是这个想法。没有什么能阻止你 echo'从你的控制器中输出一个巨大的未最小化的 JavaScript 应用程序。那完全不是 MVC。

C 用于控制器。控制器调用并向视图发送数据,接收从视图发送的数据,获取该数据并将其发送到模型以保存在数据库中(尽管 CodeIgniter 也不以任何方式强制执行此操作;如果您想保存,可以数据直接从控制器到数据库,但这显然也破坏了 MVC 主体),从数据库中检索数据并将其发送到视图以进行显示。无论如何,这些都是基础。

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2015-05-15
    • 1970-01-01
    • 2017-05-02
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多