CI表单中遇到错误

时间:2016-05-10 23:16:26

标签: php forms codeigniter

我是Codeigniter的新手,Iam通过观看视频来学习它,导师做的和我做的一样,但它给了我这样的错误"你要求的动作是不允许的。" ,它与他合作,我不知道为什么,任何帮助!

这是我的控制器代码

public function index(){

    if($this->input->post('submit')){ 

        echo $this->input->post('first_name');

    }

    $this->load->view('forms'); 
}

这是我的查看代码

<form method="POST">

<input type="text" name="first_name" />
<input type="submit" name=submit" />

</form>

2 个答案:

答案 0 :(得分:1)

使用form_open()帮助器自动添加带有CSRF值的隐藏输入。

所以你的观点应该是:

<?php echo form_open('action_url'); ?>

<input type="text" name="first_name" />
<input type="submit" name=submit" />

<?php echo form_close(); ?>

禁用CSRF保护也有效,但这不是一个好主意。

答案 1 :(得分:0)

你几乎是对的,你需要在你的表单中添加一些像动作一样的部分,并在控制器中设置或清空

class Test_form extends CI_Controller{
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $this->load->view('form_test');
    }

    //using your example. good
    public function check_form(){
        if( isset($this->input->post('first_name', TRUE))){
            echo "success <br>$$this->input->post('fisrt_name', TRUE)";
        }
        else{
            echo "error";
        }
    }

    //using form_validation. best
    public function check_form_validation(){
        $this->load->library('form_validation');

        $this->form_validation->set_rules('first_name', 'first Name', 'trim|required|xss_clean');

        if( ! $this->form_validation->run()){
            echo "error <br>" . validation_errors();
        }
        else{
            echo "success <br>$$this->input->post('fisrt_name', TRUE)";
        }
    }
}

form_test.php

first method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form">
    <input type="text" name="first_name">
    <input type="submit" value="test">
</form>
<hr>
second method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form_validation">
    <input type="text" name="first_name">
    <input type="submit" value="test">
</form>
相关问题