Welcome Guest, Not a member yet? Register   Sign In
Simple Form Error - New to CodeIgniter but not PHP
#1

[eluser]Unknown[/eluser]
I am looking at translating a current project of mine from its messy state to the codeigniter system and I am just screwing around with some stuff until I get comfortable with it.

I am trying to do a simple form, a registration form. Right now I just want to verify fields.

Controller > register.php:

Code:
<?php

class Register extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
        
        $rules['email'] = "required|valid_email";
        $rules['password'] = "required|min_length[5]|max_length[25]|matches[passconf]";
        $rules['passconf'] = "required";
        
        $this->validation->set_rules($rules);
        
        $fields['email'] = 'Email';
        $fields['password'] = 'Password';
        $fields['passconf'] = 'Password Confirmation';
        
        $this->validation->set_fields($fields);
        
        if($this->validation->run() == FALSE)
        {
            $this->load->view('regform');
        }
        else
        {
            $this->load->view('welcome_message');
        }
    }
}
?>

Views > regform.php
Code:
<html>
<head>
    <title>Form Title</title>
</head>
<body>
    
    <?=$this->validation->error_string; ?>
    <?=form_open('register')?>
    
    <h5>Email</h5>
    &lt;input type="text" name="email" value="&lt;?=$this-&gt;validation->email;?&gt;" size="50" />
    
    <h5>Password</h5>
    &lt;input type="text" name="password" value="&lt;?=$this-&gt;validation->password;?&gt;" size="50" />
    
    <h5>Password Confirm</h5>
    &lt;input type="text" name="passconf" value="&lt;?=$this-&gt;validation->passconf;?&gt;" size="50" />
    
    <div>&lt;input type="submit" value="Submit" /&gt;</div>
    
    &lt;/form&gt;
    
&lt;/body&gt;
&lt;/html&gt;

However when I fill out the form, or heck even leave it blank it takes me from my original URL: http://127.0.0.1/~steve/index.php/register/ to this:http://127.0.0.1/~steve/index.php/register/index.php/register (it throws on a extra index.php/register with the URL, or if I start with just /index.php/register (no trailing slash) it takes me to /index.php/register/index.php (throws on a extra index.php).

I am sure this is end user error, can anyone kindly point me to the source of my error.

Thanks!

-Steve
#2

[eluser]tinawina[/eluser]
Just a thought, but usually what I do is set up a specific method to handle a form. Where you have everything happening in your index() method, which is the default method that will be called when you call up your controller, you might try adding in a handleRegistration method or the like. Then bounce all of your validation and processing code into that method. Also you don't have a constructor set up.

Maybe try something like this in your controller:

Code:
class Register extends Controller {
    
    function Register() // this is your constructor which you should have in every controller. If you are using php5 you can use __constructor in place of in this instance Register.
    {
        parent::Controller();
        $this->load->helper(array('form', 'url')); // loading these helpers here since you will use them in both of the following methods.  Whatever you put in the constructor is available throughout this class.
    }

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

     function doRegistration()
     {
        $this->load->library('validation');

        $rules['email'] = "required|valid_email";
        $rules['password'] = "required|min_length[5]|max_length[25]|matches[passconf]";
        $rules['passconf'] = "required";
        
        $this->validation->set_rules($rules);
        
        $fields['email'] = 'Email';
        $fields['password'] = 'Password';
        $fields['passconf'] = 'Password Confirmation';
        
        $this->validation->set_fields($fields);
        
        if($this->validation->run() == FALSE)
        {
            $this->load->view('regform');
        }
        else
        {
            $this->load->view('welcome_message');
        }
    }
}

And in your view file:

Code:
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Form Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    
    &lt;?=$this->validation->error_string; ?&gt;
    &lt;?=form_open('register/doRegistration')?&gt;
    
    <h5>Email</h5>
    &lt;input type="text" name="email" value="&lt;?=$this-&gt;validation->email;?&gt;" size="50" />
    
    <h5>Password</h5>
    &lt;input type="text" name="password" value="&lt;?=$this-&gt;validation->password;?&gt;" size="50" />
    
    <h5>Password Confirm</h5>
    &lt;input type="text" name="passconf" value="&lt;?=$this-&gt;validation->passconf;?&gt;" size="50" />
    
    <div>&lt;input type="submit" value="Submit" /&gt;</div>
    
    &lt;/form&gt;
    
&lt;/body&gt;
&lt;/html&gt;

I hope that helps, or at least gets you moving in the right direction!
#3

[eluser]Unknown[/eluser]
Thanks for the fast reply! As a current college CS major I cant believe I forgot to add a constructor. Smile

Using your code, I made some various changes to make it work (display without errors) however when I submit it the URL goes from:

http://www.phpsn.net/index.php/register/

To

http://www.phpsn.net/index.php/register/...gistration

Why is it doing this? Any ideas?
#4

[eluser]Michael Wales[/eluser]
Check to make sure your base_url in config.php is correct.




Theme © iAndrew 2016 - Forum software by © MyBB