Welcome Guest, Not a member yet? Register   Sign In
Getting post data from a from...
#1

[eluser]kuysal[/eluser]
Hi I'm new to CI and currently on learning level. I'm trying to make a simple login page in which, information is sent by a form with a post method and to be taken as function parameter (This is the only way I know to pass the form elements in CI). Here is the view page I'm using:

Code:
<head>
<title><?=$title?></title>
</head>
<body>
<table width='848' border='0' align='center'>
  <tr>
    <td bgcolor='#E9E9E9'><table width='100%' border='0'>
      <tr>
        <td width='50%' bgcolor='#FFFFFF'><div align='center'><h1>Logo</h1></div></td>
        <td width='50%' bgcolor='#FFFFFF'>
          &lt;? echo form_open('main/login'); ?&gt;
          Username:&lt;input type='text' name='username' id="username" /&gt;
          <br />
          Password:&lt;input type='password' name='password' id="password" /&gt;
          &lt;input type='submit' name='Login' value='Login' /&gt;
        </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td bgcolor='#CCCCCC'>&nbsp;</td>
  </tr>
</table>
&lt;/body&gt;
&lt;/html&gt;

and here is my main controller which holds login function:

Code:
&lt;?php

class Main extends Controller {

    function Main()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
    }
    
    function index()
    {        
        $data['title'] = 'My Blog Title';
        $data['heading'] = 'My Blog Heading';
        
        $this->load->view("main_index_view", $data);
    }
    
    function login($username = 'default', $password = 'default')
    {
        $login_data['title'] = 'Login Page';
        $login_data['username'] = $username;
        $login_data['password'] = $password;
        
        $this->load->view("main_login_view", $login_data);
    }
}
?&gt;

and finally that is my login view:

Code:
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;? echo 'You are trying to log in with the username : ' . $username;
echo '<br>And password : ' . $password;
?&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

It always displays the default parameters. Parameters sent by form can't be taken. I've checked the code many times but it still doesn't work. As I've said I'm new to CI.

Please help...
#2

[eluser]Iksander[/eluser]
First of all you need to uncomment the PHP line that creates the form element, then you need to create a closing form element tag: &lt;/form&gt;

Grab the form just like any other form through the POST array; you can do it traditionally:

Code:
$username  = $_POST['username'];

or using the codeigniter method (which can be better)

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


Be sure to read up thoroughly on Code Igniter's input class and search the forums for 'post'.

Edit: noticed you just edited to remove the comments
#3

[eluser]coolfactor[/eluser]
1. You're not closing your form element properly. Add &lt;/form&gt;.
2. You're not accessing the submitted form data properly. (Hint: It's not passed into the function parameters.)
#4

[eluser]OwanH[/eluser]
Sorry to say but your code isn't going to work. Browsers do *not* pass form elements to the script specified in the action parameter as URL segments, they pass them as query string values, and that's only if you use the GET method, and by default the form_open helper function sends data using POST method. For your username and password to be passed to your login method they have to be passed as URL segments, which like I said your form will not do. Also there a some syntax errors in your markup and PHP blocks. I've modified your code (and made it slightly more compact) to work the way you want so here it is:

Your main controller:

Code:
&lt;?php

class Main extends Controller {

    function Main()
    {
        parent::Controller();
        
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {        
        $data = array('title' => 'My Blog Title', 'heading' => 'My Blog Heading');
        
        $this->load->view("main_index_view", $data);
    }
    
    function login()
    {
        $username = $this->input->post('username') ? $this->input->post('username')
                                                   : 'default';
        $password = $this->input->post('password') ? $this->input->post('password')
                                                   : 'default';
        $login_data = array('title' => 'Login Page', 'username' => $username,
                            'password' => $password);
        
        $this->load->view("main_login_view", $login_data);
    }
}
?&gt;

Your login form view:

Code:
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<table width='848' border='0' align='center'>
  <tr>
    <td bgcolor='#E9E9E9'><table width='100%' border='0'>
      <tr>
        <td width='50%' bgcolor='#FFFFFF'><div align='center'><h1>Logo</h1></div></td>
        <td width='50%' bgcolor='#FFFFFF'>
          &lt;?=form_open('main/login');?&gt;
            Username:&lt;input type='text' name='username' id="username" /&gt;<br />
            Password:&lt;input type='password' name='password' id="password" /&gt;
            &lt;input type='submit' name='Login' value='Login' /&gt;
          &lt;/form&gt;
        </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td bgcolor='#CCCCCC'>&nbsp;</td>
  </tr>
</table>
&lt;/body&gt;
&lt;/html&gt;

Your login page view:

Code:
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;?php echo "You are trying to log in with:<br><br>username: $username<br>password: $password"; ?&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

And good luck with your learning curve. Just be sure to study the CI documentation thoroughly and have a good understanding of the PHP language, syntax, contructs and functions as well (the PHP manual can be a PHP developer's best friend).
#5

[eluser]kuysal[/eluser]
To iksander and OwanH:

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

That solved my problem. I didn't know that. I'm still curious where the hell I got the 'form elements are passed as function parameters' information Big Grin Thanks for help..

To coolfactor:

Form element is correctly closed on my original file. I posted the wrong file. Thanks for help.




Theme © iAndrew 2016 - Forum software by © MyBB