【问题标题】:how to get post data in ci如何在ci中获取发布数据
【发布时间】:2018-06-13 17:40:04
【问题描述】:

我想在我的登录功能中使用 $this->input->post['username']; 打印帖子数据;

我有一个下面给出的家庭控制器:

<?php 
class Home extends CI_Controller{
    public function index(){
        $this->load->view("home/home_view");
    }
    public function Login(){
        echo $this->input->post['username'];
    }
}
?>

但它向我显示空结果,但如果我写 print_r($_POST) 而不是 $this-&gt;input-&gt;post['username'] 它向我显示所有数据

这是我的主页视图代码

<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/bootstrap.css">
</head>
<body>
    <div class="col-lg-2"></div>
    <div class="col-lg-8" style="min-height: 500px;box-shadow: 5px 5px 15px #000; margin-top: 50px;">
        <form method="post" action="<?php echo base_url(); ?>index.php/Home/Login">
            <table class="table table-stripped">
                <tr>
                    <th colspan="2">Login form</th>
                </tr>
                <tr>
                    <td>Username</td>
                    <td><input type="text" class="form-control" name="username"></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="password" class="form-control"></td>
                </tr>
                <tr>
                    <th colspan="2"><input type="submit" value="Submit" class="btn btn-primary"></th>
                </tr>
            </table>
        </form>
    </div>
    <div class="col-lg-2"></div>
</body>
</html>

我有自动加载的表单助手和网址助手

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    希望对您有所帮助:

    使用$this-&gt;input-&gt;post('username'); 代替$this-&gt;input-&gt;post['username']

    你的登录功能应该是这样的:

    public function Login()
    {
        /* to print all field name do like this*/
        print_r($this->input->post());
    
        /* to print a particular field name do like this*/
        echo $this->input->post('username');
        echo $this->input->post('password');
    }
    

    以这样的形式使用site_urlbase_url(良好做法):

    <form method="post" action="<?php echo site_url('Home/Login'); ?>">
          ........
    </form>
    

    更多:https://www.codeigniter.com/user_guide/libraries/input.html

    【讨论】:

      【解决方案2】:

      尽管我已经更新了您的视图文件供您参考,但将您的 HTML 从转换为 CI 表单助手的方式请参阅表单助手 [https://www.codeigniter.com/userguide3/helpers/form_helper.html][1]

      <!DOCTYPE html>
      <html>
      <head>
          <title>Home Page</title>
          <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/bootstrap.css">
      </head>
      <body>
          <div class="col-lg-2"></div>
          <div class="col-lg-8" style="min-height: 500px;box-shadow: 5px 5px 15px #000; margin-top: 50px;">
              <?php
              echo form_open('home/login');
              ?>
                  <table class="table table-stripped">
                      <tr>
                          <th colspan="2">Login form</th>
                      </tr>
                      <tr>
                          <td>Username</td>
                          <td>
                          <?php
                          $data = array(
              'name'          => 'username',
              'id'            => 'username'
      );
      
      echo form_input($data);
                          ?>
                          <input type="text" class="form-control" name="username">
      
                          </td>
                      </tr>
                      <tr>
                          <td>Password</td>
                          <td><input type="password" name="password" class="form-control">
                                              <?php
                          $pwd = array(
              'name'          => 'password',
              'id'            => 'password'
      );
      
      echo form_password($pwd);
                          ?>
                          </td>
                      </tr>
                      <tr>
      
                          <?php echo form_submit('submit', 'Submit'); ?>
                          </th>
                      </tr>
                  </table>
              <?php echo form_close();?>
          </div>
          <div class="col-lg-2"></div>
      </body>
      </html>
      

      【讨论】:

      • 是否必须从 ci 表单助手制作 html
      • @vivekmodi 不,这不是强制性的,但绝对是使用助手创建表单的好习惯,因为它可以帮助您稍后管理验证、显示错误消息等,否则您需要编写更多代码哪个 CI 已经为你做了 :)
      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多