如何访问控制器内视图提交的数据?

时间:2013-04-24 16:21:12

标签: php codeigniter scope variable-assignment

我有一个非常小的应用程序,它需要一个id,然后根据该id更新数据库表。 id是视图中的输入。

基本上我的数据库(模型)有getAllProjects()approveProject($id)

控制器:

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

class ApproveProject extends CI_Controller {

    public function index () {
        $this->loadView();
    }

    public function getData () {
        $this->load->model("db_Projects");
        $this->db_Projects->getAllProjects();

    }

    public function updateDB () {
        // how can I get this variable?
        $this->db_Projects->approveProject($toApprove); 
    }

    public function loadView() {
        $this->load->view("ViewProjectApproval");
    }
}

?>

查看:

<html language="en">
<head>
    <title>Aprobare Proiect</title>
</head>
    <h1> Aprobare Proiect </h1>
<body>
<div id="container">

    <?php
        if (isset($_POST['projectSubmit']) && ($_POST['projectSubmit'] == "Submit")) 
        { 
            $toApprove= $_POST['projectId'];
        }
    ?>


    <form action ="updateDB" method="post">
        <input type="text" name="projectId">
        <input type="submit" name="projectSubmit" value="Submit">
    </form>


</div>

</body>

我不知道我的观点是否合适,或者是否应该如此......我怎样才能将ID发送给我的控制器?

1 个答案:

答案 0 :(得分:1)

您正在使用POST方法提交表单,以便您可以从控制器方法中的POST检索变量,例如

$toApprove = $this->input->post('projectId');

所以你的方法/功能看起来像这样

public function updateDB () {
    if($this->input->post('projectId'))
    {
        $toApprove = $this->input->post('projectId');
        // load the model and call the method, like
        $this->load->model('model_name');
        $model_name->method_name($toApprove);
    }
}

<强>更新 您的表单action似乎不对,请尝试此操作(controller_name / method_name)

<form action ="<?php echo site_url('ApproveProject/updateDB') ?>" method="post">