Welcome Guest, Not a member yet? Register   Sign In
Submit simple form don't work.
#1

[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>&lt;?=$this->session->flashdata('message')?&gt;</p>
&lt;?php endif; ?&gt;

&lt;? $attributes=array('name' => 'formLogin','class' => 'formLogin', 'enctype' => 'application/x-www-form-urlencoded'); ?&gt;
&lt;?=form_open('c_loginsyslogin/index/', $attributes)?&gt;
        <p>Por favor, identifique-se:</p>

        <p>
            &lt;?=form_label('Login:', 'user_name')?&gt;
            &lt;?
            //echo form_error("txtusername");
            $formBuildingData= array(
                'type' => "text",
                'name' => "txtusername",
                'class' => "normalText",
                'size' => "25"
                );
            ?&gt;
            &lt;?=form_input($formBuildingData)?&gt;
        </p>

        <p>
            &lt;?=form_label('Senha:', 'user_pass')?&gt;
            &lt;?
            //echo form_error("txtpasswordd");
             $formBuildingData2= array(
                'type' => "password",
                'name'=> "txtpassword",
                'class' => "normalText",
                'size' => "25"
            );
            ?&gt;
            &lt;?=form_password($formBuildingData2)?&gt;
        </p>

        <p>
            &lt;?
            $formBuildingData3=array(
            'type' => "submit",
            'name' => "bntEnter",
            'class'=> "buttonNormal",
            'value'=> "Entrar"
            );
            ?&gt;
            &lt;?=form_submit($formBuildingData3)?&gt;
        </p>

&lt;?=form_close();?&gt;

config
Code:
$config['index_page'] = 'index.php';
#2

[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:
&lt;?=form_open('c_loginsyslogin/index/', $attributes)?&gt;

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,
Code:
echo 'para';
        exit;

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.
#3

[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:
&lt;?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 */

?&gt;

v_loginsyslogin
Code:
&lt;?php if($this->session->flashdata('message')) : ?&gt;
    <p>&lt;?=$this->session->flashdata('message')?&gt;</p>
&lt;?php endif; ?&gt;

&lt;? $attributes=array('name' => 'formLogin','class' => 'formLogin', 'enctype' => 'application/x-www-form-urlencoded'); ?&gt;
&lt;?=form_open('c_loginsyslogin/index/', $attributes)?&gt;
        <p>Por favor, identifique-se:</p>

        <p>
            &lt;?=form_label('Login:', 'user_name')?&gt;
            &lt;?
            //echo form_error("txtusername");
            $formBuildingData= array(
                'type' => "text",
                'name' => "txtusername",
                'class' => "normalText",
                'size' => "25"
                );
            ?&gt;
            &lt;?=form_input($formBuildingData)?&gt;
        </p>

        <p>
            &lt;?=form_label('Senha:', 'user_pass')?&gt;
            &lt;?
            //echo form_error("txtpasswordd");
             $formBuildingData2= array(
                'type' => "password",
                'name'=> "txtpassword",
                'class' => "normalText",
                'size' => "25"
            );
            ?&gt;
            &lt;?=form_password($formBuildingData2)?&gt;
        </p>

        <p>
            &lt;?
            $formBuildingData3=array(
            'type' => "submit",
            'name' => "bntEnter",
            'class'=> "buttonNormal",
            'value'=> "Entrar"
            );
            ?&gt;
            &lt;?=form_submit($formBuildingData3)?&gt;
        </p>

&lt;?=form_close();?&gt;

and this is the main template

Code:
<!doctype html>
&lt;html&gt;
&lt;?
$this->load->helper('html');
echo link_tag('CSS/adminsys.css'); ?&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;!-- navigation --&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <nav>  
        </nav>  
        <section id="maincontent">
            &lt;?
            $this->load->view($varContent);
            ?&gt;
            <article class="blogPost">
                &lt;header&gt;
                    
                &lt;/header&gt;
                    
            </article>
        </section>
        <footer>
            &lt;!-- footer --&gt;
        </footer>
    &lt;/body&gt;
&lt;/html&gt;
#4

[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?
#5

[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?
#6

[eluser]Rok Biderman[/eluser]
Yes, could be .htaccess. Try this one, worked well enough for me.
#7

[eluser]Marcone Ramos[/eluser]
Coccodrillo, thank you.
I did what you suggested and now the .htaccess is working fine, and the forms too.




Theme © iAndrew 2016 - Forum software by © MyBB