【问题标题】:Codeigniter no data sent posted on formCodeigniter 未在表单上发送数据
【发布时间】:2016-04-09 21:58:17
【问题描述】:

我的登录系统突然停止工作,我无法修复它,我一直在研究它,似乎数据没有回传,但我找不到问题。

这是我的看法:

    <?php if(! is_null($err)) echo "<div class=\"alert alert-danger\">".$err."</div>";?>
    <form class="form-signin" action="<?php echo site_url("user/validate"); ?>" method="post">
    <h2 class="form-signin-heading">Please sign in</h2>
    <label for="email" class="sr-only">Email address</label>
    <input type="email" name="email" class="form-control" placeholder="Email address" required autofocus>
    <br/>
    <label for="password" class="sr-only">Password</label>
    <input type="password" name="password" class="form-control" placeholder="Password" required>
    <br/>

    <button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign in</button>
  </form>
</div>

这是我在用户控制器中的验证函数:

public function validate()
{
    $email = $this->input->post('email');
    $password = $this->input->post('password');
    // Load the model
    echo "$email and $password sent";
    print_r($this->input->post());
    exit();
    $this->load->model('User_model');
    // Validate user's login details
    $result = $this->User_model->validate($email, $password);
    // Now we verify the result
    if($result == 0){
        // If user did not validate, then show them login page again
        $err = 'Invalid email and/or password.';
        $this->login($err);
    }else{
        // If user did validate send him to homepage after setting the credentials
        $this->session->set_userdata($result);
        redirect(site_url("/index/index"));
    }
}

echo 和 print_r 显示没有发布任何数据。

经过一番研究,我发现问题应该是.htaccess文件,这是我当前的.htaccess:

RewriteEngine On
RewriteRule ^(application) - [F,L] 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

但它不起作用。

【问题讨论】:

  • 不,$this->input->post() 只是空的
  • 这意味着你的表单操作是错误的,因为值没有进入验证函数
  • 查看表单动作的路径
  • 如果你的url中有index.php,使用site_url(),如果你的url中没有index.php,使用base_url()。

标签: php .htaccess codeigniter codeigniter-3


【解决方案1】:
<form class="form-signin" action="<?php echo site_url("user/validate"); ?>" method="post">

将其更改为以下内容:

<form class='form-signin' action="<?php echo base_url();?>user/validate" method="post" name="user_from">

也不要将提交按钮名称用作“提交”。使用 sbt 或其他名称,id 也一样...

【讨论】:

    【解决方案2】:
    1. 你有一个exit(); 在验证函数注释它
    2. 试试:if(!empty( $err ))

    3. 试试:base_url("index.php/user/validate");

    4. try : 在里面加载模型

      public function __construct()

      不在内部

      public function validate()

    【讨论】:

    • 感谢您的回答!我达到了那个功能,问题是 $this->input->post() 完全是空的。
    猜你喜欢
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2018-09-24
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多