如何在ci中获取发布数据

问题描述:

i want to print post data in my login function with $this->input->post['username'];

I have a home controller given below :

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

but its showing me null result but if i write print_r($_POST) instead of $this->input->post['username'] its showing me all data

this is my home view code

<!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>

i had auto loaded form helper and url helper

Hope this will help you :

Use $this->input->post('username'); instead of $this->input->post['username'] :

Your Login function should be like this :

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');
}

Use site_url or base_url in form like this ( good practice) :

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

for more : https://www.codeigniter.com/user_guide/libraries/input.html

Convert your HTML from into CI form helper way although i have updated your view file for your reference see form helper [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>