无法从任何表单获取任何$ _POST数据

时间:2015-12-02 01:15:44

标签: codeigniter

我尝试了很多谷歌点击,但我还是无法从表单中检索任何POST数据,$ _POST只是一个空数组array(0) { }。我发现很少有帖子说问题出现在.htaccess中,它没有正确地重定向我的POST数据,但这些帖子对我没什么帮助。我已经尝试了很多我发现的.htaccess文件,但没有一个用于POST数据。

我正在使用Debian Jessie和Apache 2.4.10,PHP 5.6.14-0 + deb8u1。我的VHOST文件中AllowOverride all内有<Directory。 CodeIgniter 3.0.3。我在index.php的顶部有var_dump($_POST);进行测试。我也启用了rewrite_module。 apache2 error.log文件中没有错误。

这是我的设置:

控制器:Admin.php

<?php
class Admin extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->helper('form');
        $this->load->library('session');
        $this->load->library('form_validation');

    }

    public function index()
    {

        if(isset($this->session->userdata['logged']))
        {
            $this->load->view('admin/dashboard');
        }
        else
        {
            $this->load->view('templates/default/header');
            $this->load->view('admin_login');
            $this->load->view('templates/default/footer');
        }

    }

    public function login()
    {

        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if($this->form_validation->run() == FALSE)
        {
            echo "Validation failed!";
        }
        else
        {
            echo "Validation success!";
        }


    }
}

查看:admin_login.php:

<form class="form-signin" method="post" action="/admin/login/">
<h2 class="form-signin-heading">Kirjaudu sisään</h2>
<label for="inputEmail" class="sr-only">Sähköpostiosoite</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<label for="inputPassword" class="sr-only">Salasana</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Salasana" required>
<div class="checkbox">
    <label>
        <input type="checkbox" value="remember-me"> Muista minut
    </label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Kirjaudu</button>

当前.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

2 个答案:

答案 0 :(得分:1)

您的操作错误。应该是这样的

action="<?php echo base_url()?>admin/login/">

以及在助手中加载网址

$this->load->helper('form','url');

以及这些输入标签中有无名称属性

<input type="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<input type="password" id="inputPassword" class="form-control" placeholder="Salasana" required>

这样做

在控制器中

$this->load->helper('form'); #load library 

在视图中

<?

$attributes = array('class' => 'form-signin');
echo form_open('admin/login/', $attributes);

$data1 = array(
              'id'                  => 'inputEmail',
              'class'               => 'form-control',
              'Placeholder'         => 'Sähköpostiosoite',
              'name'                => 'email',
            );

echo form_input($data1);

$data2 = array(
              'id'                  => 'inputPassword',
              'class'               => 'form-control',
              'Placeholder'         => 'Salasana',
              'name'                => 'password',
            );

echo form_password($data2);

echo form_submit('submit', 'Log In');
echo form_close();

答案 1 :(得分:1)

试试这个: 在控制器中添加$this->load->helper(array('form','url'));

中的function __construct()

在视图中:

<form class="form-signin" method="post" action="<?php echo base_url()?>admin/login">
    <h2 class="form-signin-heading">Kirjaudu sisään</h2>
    <label for="inputEmail" class="sr-only">Sähköpostiosoite</label>
    <input type="email" name="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
    <label for="inputPassword" class="sr-only">Salasana</label>
    <input type="password" name = "password" id="inputPassword" class="form-control" placeholder="Salasana" required>
    <div class="checkbox">
        <label>
            <input type="checkbox" value="remember-me"> Muista minut
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Kirjaudu</button>

希望这有帮助! ^^