1

I'm using PHP Framework CodeIgniter.

View

<?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(); ?> 

When I try to submit this page it goes to: http://mywebsite.com/admin/index.php/login/connect

Controller

class Login extends 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> 

But it returns an empty array.

Can anyone suggest a method to fix this?

Thanks.

7
  • 1
    you didnt specified the method, so it should be a get request Commented Nov 7, 2014 at 7:15
  • Show your Controller. Commented Nov 7, 2014 at 7:19
  • 3
    @oPi you don't need to specified method in codeigniter, Its post by default. Check ellislab.com/codeigniter/user-guide/helpers/form_helper.html Commented Nov 7, 2014 at 7:20
  • why you are including .php extension? $this->load->view('login.tpl.php'); Commented Nov 7, 2014 at 7:28
  • @jogesh_pi didnt know. Sorry about the mistake so Commented Nov 7, 2014 at 7:56

4 Answers 4

0

I think he is using CodeIgniter, so the form_open() will place the method="post" automatically. Please can you share the output HTML of the form?

Check that you aren't redirecting your request, or the post variables will be resetted. Try print_r instead of var_dump...

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, I've tried print_r and it doesn't worked (still getting empty data).
After capturing the request sent I got the following from the header: Status Code:301 Moved Permanently Is this the issue? If yes how to avoid this (is a good tutorial available for this)? Thanks in advance.
Status Code 301 represents a permanent redirect. It should be the problem. Track your request and understand "where" the redirect is. When a redirect happens the POST variables are resetted. You should see your $_POST just before the redirect. I'm sorry but i'm not really experienced with CodeIgniter, but i think the problem is right there.
0

don't use $_POST Try using

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

5 Comments

Yes, correct method is $this->input->post() but $_POST will also working. If OP don't get output throght $_POST then same happen with $this->input->post()
@jogesh_pi After capturing the request sent I got the following from the header: Status Code:301 Moved Permanently Is this the issue? If yes how to avoid this (is a good tutorial available for this)? Thanks in advance.
@jogesh_pi Also I've updated my code to the method mentioned above.
@nikhil take a look on this post stackoverflow.com/questions/6007928/…
@jogesh_pi Thanks, identified the issue. It is related to an unwanted redirection.
0

Identified the issue. It was related to the website url, and there was an unwanted 301 redirect when the form submits. Reference: $this->input->post() always returning FALSE, due to an unwanted 301

Comments

0

Enter folowing class after parent::__construct();

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.