[eluser]Marcone Ramos[/eluser]
I am following a tutorial about creating a simple login form. All I know is that nothing happens when I submit the form. It loads the same page, like the submit had no effect.
Can you guys help me please? I just can't see what's wrong.
the controller:
Code: class C_loginsyslogin extends CI_Controller{
//creating the index, the default function in my class
function index() {
//Load Libraries
$this->load->library(array('encrypt', 'form_validation', 'session'));
// LOAD HELPERS
$this->load->helper(array('form', 'url'));
// VALID USER CREDENTIALS
$user_credentials = array();
$user_credentials['testuser1'] = array(
'user_name' => 'testuser1',
'user_pass' => 'Gag/R6LlMz8JKhjd+pkMrL+MUIHn86vjs/ZJ31uH+QRCh1eRxxA0Fve6FXfE7rmFqgqsiwe2ZFrFT8ylZs050A==' // password
);
$user_credentials['testuser2'] = array(
'user_name' => 'testuser2',
'user_pass' => 'Gag/R6LlMz8JKhjd+pkMrL+MUIHn86vjs/ZJ31uH+QRCh1eRxxA0Fve6FXfE7rmFqgqsiwe2ZFrFT8ylZs050A==' // password
);
$user_credentials['testuser3'] = array(
'user_name' => 'testuser3',
'user_pass' => 'Gag/R6LlMz8JKhjd+pkMrL+MUIHn86vjs/ZJ31uH+QRCh1eRxxA0Fve6FXfE7rmFqgqsiwe2ZFrFT8ylZs050A==' // password
);
//---------------- SUBMISSION --------------------
//set_rules(THE NAME OF THE FIELD, THE NAME THAT USER SHOULD SEE THEM, IF IT'S REQUIRED OR NOT);
$this->form_validation->set_rules('txtusername','User Name','required');
$this->form_validation->set_rules('txtpassword','Password','required');
//--------------- VALIDATING --------------------
// has the form been submitted and with valid form info (not empty values)
if($this->input->post('bntEnter'))
{
echo 'para';
exit;
if($this->form_validation->run())
{
$user_name = $this->input->post('txtusername');
$user_pass = $this->input->post('txtpassword');
if(array_key_exists($user_name, $user_credentials))
{
// continue processing form (validate password)
}
else
{
$this->session->set_flashdata('message', 'A user does not exist for the username specified.');
redirect('c_/index/');
}
}
}
//-----------------END VALIDATING ---------------
//----------------- END SUBMISSION ------------
$loadTemp["varContent"]="v_loginsyslogin";
$this->load->view('v_admintemplate', $loadTemp);
}
}
the view file
Code: <?php if($this->session->flashdata('message')) : ?>
<p><?=$this->session->flashdata('message')?></p>
<?php endif; ?>
<? $attributes=array('name' => 'formLogin','class' => 'formLogin', 'enctype' => 'application/x-www-form-urlencoded'); ?>
<?=form_open('c_loginsyslogin/index/', $attributes)?>
<p>Por favor, identifique-se:</p>
<p>
<?=form_label('Login:', 'user_name')?>
<?
//echo form_error("txtusername");
$formBuildingData= array(
'type' => "text",
'name' => "txtusername",
'class' => "normalText",
'size' => "25"
);
?>
<?=form_input($formBuildingData)?>
</p>
<p>
<?=form_label('Senha:', 'user_pass')?>
<?
//echo form_error("txtpasswordd");
$formBuildingData2= array(
'type' => "password",
'name'=> "txtpassword",
'class' => "normalText",
'size' => "25"
);
?>
<?=form_password($formBuildingData2)?>
</p>
<p>
<?
$formBuildingData3=array(
'type' => "submit",
'name' => "bntEnter",
'class'=> "buttonNormal",
'value'=> "Entrar"
);
?>
<?=form_submit($formBuildingData3)?>
</p>
<?=form_close();?>
config
Code: $config['index_page'] = 'index.php';
[eluser]Rok Biderman[/eluser]
Hello Marcone and welcome to the CI forums. There are 3 things about this that strike me as odd.
You say the submit is redirecting you to the same page
Code: <?=form_open('c_loginsyslogin/index/', $attributes)?>
It's exactly what it should do, you have to make an action to a different controller/method if you want that. But that is ok in my view.
Secondly, the script exits here,
so you have to remove the exit command if you want further processing.
And last, you redirect to c_/index (which is a protected page I assume) only if the user doesn't exist. If you want a redirect when there is a valid user, you need to put it here and delete the one from else part.
Code: if(array_key_exists($user_name, $user_credentials))
{
// continue processing form, i'm just redirecting
redirect('c_/index/');
}
If you make these 3 changes, it only redirects if the username is valid, if the input is present it just says 'para' (probably for your debugging) and sets the message.
I just wanted to add that if you do it like this, you will need to build 2 separate processings for something that can be done in one as efficiently. You already check if the username (array_key) is present. Now you will have to do it once more for the user and pass matching. I don't know the rest of your code, but it seems redundant.
[eluser]Marcone Ramos[/eluser]
Thank you very much Coccodrillo.
I sorry for the mess I did with the code, I should have fixed it before sending and keep it simple so you could see what's my real problem. I'm not a native english speaker so I'll try explain again what's going on.
In the code bellow, when I click ENTRAR, it should be redirected to Code: c_loginsyslogin/index/
and execute what's inside of Code: if($this->input->post('bntEnter'))
{
echo "It works!";
exit;
}
, but that's not what happens for some reason. it reloads the form page, instead of show the "it works".
I'm posting a simple code again, so it will make easier for you guys to understand what's going on:
C_loginsyslogin
Code: <?php
class C_loginsyslogin extends CI_Controller{
function index()
{
// LOAD LIBRARIES
$this->load->library(array('encrypt', 'form_validation', 'session'));
// LOAD HELPERS
$this->load->helper(array('form'));
if($this->input->post('bntEnter'))
{
echo "It works!";
exit;
}
$loadTemp['varContent']='v_loginsyslogin';
$this->load->view('v_admintemplate', $loadTemp);
}
}
/* End of file main.php */
/* Location: ./application/controllers/main.php */
?>
v_loginsyslogin
Code: <?php if($this->session->flashdata('message')) : ?>
<p><?=$this->session->flashdata('message')?></p>
<?php endif; ?>
<? $attributes=array('name' => 'formLogin','class' => 'formLogin', 'enctype' => 'application/x-www-form-urlencoded'); ?>
<?=form_open('c_loginsyslogin/index/', $attributes)?>
<p>Por favor, identifique-se:</p>
<p>
<?=form_label('Login:', 'user_name')?>
<?
//echo form_error("txtusername");
$formBuildingData= array(
'type' => "text",
'name' => "txtusername",
'class' => "normalText",
'size' => "25"
);
?>
<?=form_input($formBuildingData)?>
</p>
<p>
<?=form_label('Senha:', 'user_pass')?>
<?
//echo form_error("txtpasswordd");
$formBuildingData2= array(
'type' => "password",
'name'=> "txtpassword",
'class' => "normalText",
'size' => "25"
);
?>
<?=form_password($formBuildingData2)?>
</p>
<p>
<?
$formBuildingData3=array(
'type' => "submit",
'name' => "bntEnter",
'class'=> "buttonNormal",
'value'=> "Entrar"
);
?>
<?=form_submit($formBuildingData3)?>
</p>
<?=form_close();?>
and this is the main template
Code: <!doctype html>
<html>
<?
$this->load->helper('html');
echo link_tag('CSS/adminsys.css'); ?>
<head>
<title><!-- navigation --></title>
</head>
<body>
<nav>
</nav>
<section id="maincontent">
<?
$this->load->view($varContent);
?>
<article class="blogPost">
<header>
</header>
</article>
</section>
<footer>
<!-- footer -->
</footer>
</body>
</html>
[eluser]Rok Biderman[/eluser]
sorry for delay in answering, haven't been here a lot past few days. For me your code works perfectly, could there be some server issue?
[eluser]Marcone Ramos[/eluser]
no problem man. thanks for answering.
I looked at the .htaccess and they have just one single line that says "deny from all".
Do you think that can be the problem? Is that normal?
I tested Codeigniter on another domain I have at the same server, and yes, it seems to be a server issue.
I created a simple post php function and it worked fine, but after installing codeigniter it stoped working. Do you think it can be the .htaccess?
[eluser]Rok Biderman[/eluser]
Yes, could be .htaccess. Try this one, worked well enough for me.
[eluser]Marcone Ramos[/eluser]
Coccodrillo, thank you.
I did what you suggested and now the .htaccess is working fine, and the forms too.
|