axmug
axmug

Reputation: 466

Post variables in Codeigniter

I have a view in Codeigniter that is a form written in HTML directly.

<form action="http://localhost/index.php/cindice/mostrar" mehtod="post">
<label for="usuario" id="usr">Usuario</label>
<input type="text" name="usuario" /><br /><br />
<label for="contrasenya" id="ctr">Contrase&ntilde;a</label>
<input type="password" name="contrasenya" id="ctr" />
<label for="acceder"></label><br /><br />
<input type="submit" name="acceder" id="bacceder" value="Entrar" /> 
</form>

The form has two fields: usuario (user) and contraseña (password).

And it is the controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cindice extends CI_Controller {


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

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

public function mostrar()
{
    $mostrar=$this->input->post('usuario');
    print_r($_POST);
    echo "<br />";

}

 }
?>

When I load the page localhost/index/cindice and I write a username and a password. But the result of print_r($_POST) command is array(), what it means that any value was saved in the form.

How can I fix it?

Upvotes: 0

Views: 1575

Answers (2)

Pattle
Pattle

Reputation: 6014

I think it is empty because you have spelled the the method attribute wrong in your opening form tag. You have put

<form action="http://localhost/index.php/cindice/mostrar" mehtod="post">

When it should be

<form action="http://localhost/index.php/cindice/mostrar" method="post">

Upvotes: 2

RayZor
RayZor

Reputation: 665

Be mindful if you have CSRF switched on in your config - if it is then you need to use the codeigniter form helper and form_open() so that the hidden security value is added.

Upvotes: 0

Related Questions