CodeIgniter Forums
how do you handle form submissions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: how do you handle form submissions (/thread-2943.html)



how do you handle form submissions - El Forum - 08-31-2007

[eluser]jvittetoe[/eluser]
in my current application, once a user logins in they are redirected to their dashboard, via the dashboard.php controller. currently residing at www.myproject.com/dashboard

now from this page there is a link to modify settings. once clicked they are then routed to www.com/dashboard/settings - this function from within the dashboard controller loads a view file...
Code:
$template['mainContent'] = $this->load->view('dspSettings', $data, true);
        $this->load->view('layDash', $template);

now from the dspSettings view file there is a form. im using the ci form helper. the form is as follows...
Code:
<?=form_open('dashboard/updateUserAccount'); ?>
        <label for="modify_oldpassword">Old Passsword</label><br />
        &lt;input  type="password" name="password_old" id="modify_oldpassword" value="" /&gt;<br />
        <label for="modify_newpassword">New Passsword</label><br />
        &lt;input  type="password" name="password_new" id="modify_newpassword" value="" /&gt;<br />
        <label for="modify_confpassword">Confirm New Passsword</label><br />
        &lt;input  type="password" name="password_conf" id="modify_confpassword" value="" /&gt;<br />
        <label for="modify_firstname">First Name</label><br />
        &lt;input  type="text" name="firstname" id="modify_firstname" value="" /&gt;<br />
        <label for="modify_lastname">Last Name</label><br />
        &lt;input  type="text" name="lastname" id="modify_lastname" value="" /&gt;<br />
        <label for="">Delete Account?</label><br >
        &lt;input type="checkbox" value="0" id="modify_deleteaccount" /&gt;<label for="modify_deleteaccount" class="selectable">Check here to delete your account.</label><br />    
        &lt;input type="submit" value="Modify Settings" /&gt;
    &lt;/form&gt;

on submission, the form routes to the dashboard/updateUserAccount function:
Code:
function updateUserAccount(){
        
        $data['title'] = "My Finance ~ v1.00 ~ Dashboard";
        $data['status'] = "PLEASE LOGIN";
        
         $rules['password_old'] = 'trim|required';
          $rules['password_new'] = 'trim';
          $rules['password_conf'] = 'trim';
          $rules['firstname'] = 'trim';
          $rules['lastname'] = 'trim';
         $this->validation->set_rules($rules);
    
    
        $template['mainContent'] = $this->load->view('dspSettings', $data, true);
        $this->load->view('layDash', $template);
        
        if($this->validation->run() == FALSE){
            
                $data['status'] = "Invalid Login";
                redirect('/dashboard/settings/', 'refresh');
                
        } else {
            
            $password_old = $this->validation->password_old;
            $password_new = $this->validation->password_new;
            $password_conf = $this->validation->password_conf;
            $firstname = $this->validation->firstname;
            $lastname = $this->validation->lastname;
            
//DISREGUARD BELOW THIS LINE
//HAVENT GOT THERE YET

            if ($this->userAccountHandling->modifyUserAccount($password_old, $password_new, $password_conf, $firstname, $lastname)) {

                $data['title'] = "My Finance - v1.00";
                $data['status'] = "USER ACCOUNT FOUND - LOGGING IN";

                //USER ALREADY EXISTS
                //LOG USER IN
                if ($this->userAccountHandling->loginTheUser($emailaddress, $password) == TRUE) {
                    
                    //successful login
                    //$this->load->view('layMain');
                    redirect('/dashboard/', 'refresh');
                
                }
            }
        }
        
    }

the current url is www.com/dashboard/updateUserAccount which brings up errors telling me 'Undefined property: password_new' because that field was submitted empty. how can i keep the field optional and still modify a record in the database without running into errors like this.

im still new to the framework and developing in general. how is my structure. could i be doing these easier and faster? thanks.


how do you handle form submissions - El Forum - 09-01-2007

[eluser]Michael Wales[/eluser]
Update your model method, placing the optional parameters at the end and defining default values. Something like this:

Code:
function updateUser($username, $password, $email, $new_password = '') {
}

So, when you call that function - if you fail to define the 4th parameter it will default to ''


how do you handle form submissions - El Forum - 09-02-2007

[eluser]jvittetoe[/eluser]
this issue arises when i submit the form and call the dashboard/updateUserAccount...when it gets tothe else statement, these lines are throwing errors.
Code:
$password_old = $this->validation->password_old;
            $password_new = $this->validation->password_new;
            $password_conf = $this->validation->password_conf;
            $firstname = $this->validation->firstname;
            $lastname = $this->validation->lastname;

the errors are..
Message: Undefined property: password_old
Message: Undefined property: password_new
...and so on for all 5.

ive also updated my model function with your suggestion. but my script is failing before it even gets there.


how do you handle form submissions - El Forum - 09-03-2007

[eluser]WolfgangA[/eluser]
[quote author="jvittetoe" date="1188811043"]
...
$this->validation->password_old;
...
Message: Undefined property: password_old
[/quote]

I guess your assumption is wrong that the validation object has such a property. It "only" has the rules assigned fro the fields it should check.
See http://ellislab.com/codeigniter/user-guide/libraries/validation.html

So why do you not take the values from your original array?


how do you handle form submissions - El Forum - 09-03-2007

[eluser]jvittetoe[/eluser]
i guess im not reall sure how to take the values from the original array.


how do you handle form submissions - El Forum - 09-04-2007

[eluser]WolfgangA[/eluser]
Have you checked the $_POST array?


how do you handle form submissions - El Forum - 09-04-2007

[eluser]Michael Wales[/eluser]
Try setting the fields' names - I think that will allow you to use $this->validation->var_name (as well as prettying up your error messages).

Code:
$field['password_old'] = 'old password';
$field['password_new'] = 'new password';
$this->validation->set_fields($fields);



how do you handle form submissions - El Forum - 09-04-2007

[eluser]jvittetoe[/eluser]
Ok, i have a new idea on how to handle my logins / new registrations. upon coming to the site the default controller is welcome.php from here there is one form which submits a post array, containing an email address and a password, to the welcome/checkUser method is my welcome controller. this method checks to see if the submitted email address is already taken. if it is, redirect users to the welcome/doLogin method, if its not taken, redirect users to the welcome/doRegister method. and while inside welcome/doRegister, once the registration has returned from the model, i want to redirect users to the welcome/doLogin method to be automatically logged in. im having the hardest time getting things to work.

Code:
function checkUser(){
        $this->load->library('validation');
        $this->load->model('User_model');
        $validUser = $this->User_model->checkEmailExists($this->validation->emailaddress, $this->session->userdata('session_id'));
        
        if($validUser){
            $this->doLogin();
        } else {
            $this->doRegister();
        }
    }
Code:
function doLogin(){
        $this->load->library('validation');
        $this->load->helper( array('url', 'form', 'date') );
        $this->load->model('User_model');
        
        $user_id = $this->User_model->login($this->validation->emailaddress, $this->validation->password, $this->session->userdata('session_id'));

        if($user_id != 0){
            
            // succsessful login
            $this->session->set_userdata(array('user_id' => $user_id));
            $data['user'] = $this->User_model->getDetails($user_id);
            $data['title'] = "My Finance - v1.0 ~ Dashboard";
            
            redirect('/dashboard/overview', 'refresh');
            //$template['mainContent'] = $this->load->view('dspDashboard', $data, true);
            //$this->load->view('layDash', $template);

        } else {
            
            // login failed
            $data['title'] = "My Finance - v1.0 ~ Home";
            //redirect('/welcome', 'refresh');
            $template['mainContent'] = $this->load->view('dspHome', $data, true);
            $this->load->view('layMain', $template);
        }
    }
Code:
function doRegister(){
        $this->load->library('validation');
        $this->load->helper( array('url', 'form', 'date') );
        $this->load->model('User_model');
        
        $user_id = $this->User_model->register($this->validation->emailaddress, $this->validation->password, $this->session->userdata('session_id'));
        
        if($user_id > 0){
            
            // successful registration
            //$this->doLogin();
            $user_id = $this->User_model->login($this->validation->emailaddress, $this->validation->password, $this->session->userdata('session_id'));

            if($user_id != 0){
            
                // succsessful login
                $this->session->set_userdata(array('user_id' => $user_id));
                $data['user'] = $this->User_model->getDetails($user_id);
                $data['title'] = "My Finance - v1.0 ~ Dashboard";
        
                //redirect('/dashboard', 'refresh');
                //$template['mainContent'] = $this->load->view('dspDashboard', $data, true);
                //$this->load->view('layDash', $template);

            } else {
            
                // login failed
                $data['title'] = "My Finance - v1.0 ~ Home";
                //redirect('/welcome', 'refresh');
                $template['mainContent'] = $this->load->view('dspHome', $data, true);
                $this->load->view('layMain', $template);
            }
            
        } else {
            
            // failed registration            
            $data['title'] = "My Finance - v1.0 ~ Home";
            redirect('/welcome', 'refresh');
                
        }
    }

now my question is how can i pass my $data variable when using a redirect. because all of this is taking place in my welcome controller. so once someone logs in, they are taken to the dashboard controller. if i uncomment the redirect lines and using the load->view my url stays www.com/welcome/home instead of www.com/dashboard.