Welcome Guest, Not a member yet? Register   Sign In
User Registration
#1

[eluser]GamingFusion[/eluser]
I have created a very simple user registration. I have it check for form validation and all.

But i have a problem when the user inputs invalid data.

I have the form send the data to "addUser", where the form validation is done.

If the form is not valid, i want it to go back to "register", where it displays whats wrong with the user input.

But it stays at "addUser", and loads the "register" page within the "addUser" along with the rest of the code form "addUser". I also want addUser to pass on the validation data to register where errors are displayed.

Heres all my code to do with this

CONTROLLERS

Code:
//User Registration
    function register()
    {
        
        //Load Data
        $data['title'] = "User Registration";
        $data['heading'] = "User Registration";
        
        //load models
        $this->load->model('database');
        
        $this->load->view('register', $data);
    }
    
        //Add the User to the Database
    function addUser()
    {
        
        //Load Libraries
        $this->load->library('form_validation');
        
        //Form Validation
        $this->form_validation->set_rules('first', 'First Name', 'required');
        $this->form_validation->set_rules('last', 'Last Name', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required|callback_username_check|min_length[5]|max_length[20]');
        $this->form_validation->set_rules('password', 'Password', 'required|max_length[20]|matches[passconf]|sha1');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('confirm', 'Confirm Code', 'required');
                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('register', $data);
        }
        else
        {
            //Run Database Query
        $this->database->register();
        }
        
        //Load Data
        $data['title'] = "Add User";
        
        //Load the View File
        $this->load->view('addUser', $data);
    }

VIEWS

register.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
<h1>&lt;?=$heading?&gt;</h1>
Fill out the following form to register. All fields required.
<hr />
  
&lt;?php echo validation_errors(); ?&gt;

&lt;?=form_open('theater/addUser');?&gt;

<table>
    <tr>            
        <td>Name: </td><td>&lt;input name="first" class="input" type="text" value="&lt;?php echo set_value('first'); ?&gt;" /&gt;
            &lt;input name="last" class="input" type="text" value="&lt;?php echo set_value('last'); ?&gt;" /&gt;
        </td>
    </tr>
    <tr>
        <td>Username: </td><td>&lt;input name="username" class="input" type="text" value="&lt;?php echo set_value('username'); ?&gt;" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>Password: </td><td>&lt;input name="password" class="input" type="text" /&gt;
            &lt;input name="passconf" class="input" type="text" /&gt;
        </td>
    </tr>
    <tr>
        <td>Confirm Code: </td><td>&lt;input name="confirm" class="input" type="text" /&gt;&lt;/td>
    </tr>
    <tr>
        <td>&lt;?=form_submit('submit', 'Register');?&gt;</td>
    </tr>
</table>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

addUser.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
    
    if ($register = TRUE) {
        echo '<h1>Success!</h1>
              <h3>You have Been Successfully Registered</h3>';
    }else{
        echo '<h1>Failure!</h1>
        <h3>TYou have Not been Registered. Please email the administrator</h3>';
    }
    
?&gt;    
&lt;/body&gt;
&lt;/html&gt;

MODELS
Code:
function register()
    {
        $first = $this->input->post('first');
        $last = $this->input->post('last');
        $username = $this->input->post('username');
        $password =  $this->input->post('password');
        $passconf = $this->input->post('passconf');
        $confirm = $this->input->post('confirm');
        
        $data = array('first' => $first, 'last' => $last, 'username' => $username, 'password' => $password);
        
        $query = $this->db->insert('users', $data);
        
        if ($query) {
            return $data['register'] = TRUE;
        }else{
            return $data['register'] = FALSE;
        }
        
    }
#2

[eluser]clip[/eluser]
Code:
function addUser()
    {
        
        //Load Libraries
        $this->load->library('form_validation');
        
        //Form Validation
        $this->form_validation->set_rules('first', 'First Name', 'required');
        $this->form_validation->set_rules('last', 'Last Name', 'required');
        $this->form_validation->set_rules('username', 'Username', 'required|callback_username_check|min_length[5]|max_length[20]');
        $this->form_validation->set_rules('password', 'Password', 'required|max_length[20]|matches[passconf]|sha1');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('confirm', 'Confirm Code', 'required');
                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('register', $data);
        }
        else
        {
            //Run Database Query
        $this->database->register();
        }
        // This is one of your problems... The view below will always load
        //   everytime you navigate to addUser page
        //Load Data
        $data['title'] = "Add User";
        
        //Load the View File
        $this->load->view('addUser', $data);
    }




Theme © iAndrew 2016 - Forum software by © MyBB