【问题标题】:Form submit returns empty data表单提交返回空数据
【发布时间】:2015-01-03 22:25:16
【问题描述】:

我正在使用 PHP 框架 CodeIgniter。

查看

<?php echo form_open('login/connect'); ?>
    <table border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td><?php echo form_label('Username', 'username'); ?></td>
            <td><?php echo form_input(array('id'=>'username', 'name'=>'username')); ?></td>
        </tr>
        <tr>
            <td><?php echo form_label('Password', 'password'); ?></td>
            <td><?php echo form_password(array('id'=>'password', 'name'=>'password')); ?></td>
        </tr>
        <tr>
            <td></td>
            <td><?php echo form_submit('login_submit', 'Valider'); ?></td>
        </tr>
    </table>
<?php echo form_close(); ?>

当我尝试提交此页面时,它会转到:http://mywebsite.com/admin/index.php/login/connect

控制器

类登录扩展 CI_Controller {

function __construct()
{
    parent::__construct();
}

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

function connect()
{
    $username= $this->input->post('username');
    $password=$this->input->post('password');
    echo $username.', '.$password;
}

}

HTML

<div id="login">
    <div id="login-inner">
        <h1>Login</h1>
        <form action="http://myswebsite.com/admin/index.php/login/connect" method="post" accept-charset="utf-8">                    
            <table border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><label for="username">Username</label></td>
                    <td><input type="text" name="username" value="" id="username"  /></td>
                </tr>
                <tr>
                    <td><label for="password">Password</label></td>
                    <td><input type="password" name="password" value="" id="password"  /></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="login_submit" value="Valider"  /></td>
                </tr>
            </table>
        </form>
    </div>
</div>

但它返回一个空数组。

谁能提出解决这个问题的方法?

谢谢。

【问题讨论】:

  • 你没有指定方法,所以应该是get请求
  • 显示你的控制器。
  • @oPi 你不需要在codeigniter中指定方法,默认为post。检查ellislab.com/codeigniter/user-guide/helpers/form_helper.html
  • 为什么要包含.php 扩展名? $this-&gt;load-&gt;view('login.tpl.php');
  • @jogesh_pi 不知道。很抱歉这个错误所以

标签: php codeigniter


【解决方案1】:

我认为他正在使用 CodeIgniter,所以 form_open() 会自动放置 method="post"。请问可以分享表单的输出 HTML 吗?

检查您没有重定向您的请求,否则帖子变量将被重置。 尝试 print_r 而不是 var_dump...

【讨论】:

  • 你好,我试过 print_r 但它不起作用(仍然得到空数据)。
  • 捕获发送的请求后,我从标头中得到以下信息:状态代码:301 永久移动 这是问题吗?如果是,如何避免这种情况(有一个很好的教程可用)?提前致谢。
  • 状态码 301 表示永久重定向。这应该是问题所在。跟踪您的请求并了解重定向的“位置”。当重定向发生时,POST 变量被重置。您应该在重定向之前看到您的 $_POST。很抱歉,我对 CodeIgniter 并没有真正的经验,但我认为问题就在那里。
【解决方案2】:

不要使用 $_POST 尝试使用

$username= $this->input->post('username');
$password=$this->input->post('password');

【讨论】:

  • 是的,正确的方法是$this-&gt;input-&gt;post(),但$_POST 也可以。如果 OP 没有通过$_POST 获得输出,那么$this-&gt;input-&gt;post() 也会发生同样的情况
  • @jogesh_pi 捕获发送的请求后,我从标题中得到以下信息: 状态代码:301 永久移动 这是问题吗?如果是,如何避免这种情况(有一个很好的教程可用)?提前致谢。
  • @jogesh_pi 另外,我已将代码更新为上述方法。
  • @nikhil 看看这个帖子stackoverflow.com/questions/6007928/…
  • @jogesh_pi 谢谢,发现问题。它与不需要的重定向有关。
【解决方案3】:

发现问题。 它与网站 url 相关,并且在提交表单时出现了不需要的 301 重定向。 参考:$this->input->post() always returning FALSE, due to an unwanted 301

【讨论】:

    【解决方案4】:

    在 parent::__construct() 之后进入下面的类;

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

    【讨论】:

      猜你喜欢
      • 2019-10-06
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      相关资源
      最近更新 更多