Derfder
Derfder

Reputation: 3324

If condition for 2 forms(2 submit buttons) in one controller in CI

I have a controller auth and there are 2 methods login and register.

And it is working nicely.

However, I need to have another function called login_and_register which will have an if condition that will determine which part will be executed. If the login part (basically code from login method) or register part (code from register method).

The if condition should be checking which submit button was clicked.

But here is the problem.

I need the form_open for both forms (login and register) stay like this:

<?php echo form_open('auth/login_and_register'); ?>

And this cannot be changed to e.g. auth/login_and_register_log or auth/login_and_register_reg or auth/login_and_register/log or auth/login_and_register/reg etc.

The login and register forms are in the same view and after submit is pressed this function login_and_register in auth controller is exectuted.

I was thinking that I can make the if condition base on the name of submit value, because for login form it is called submitlog and for registration form it is called submitreg.

So, maybe something like this:

if( isset($this->form_validation->set_value('submitlog')) ) {
   //code for login part
}
elseif ( isset($this->form_validation->set_value('submitreg')) ) {
   //code for registration part
}
else {
   //code for redirect to homepage
}

But it is not working. Any idea why?

Upvotes: 1

Views: 1812

Answers (2)

Francois Bourgeois
Francois Bourgeois

Reputation: 3690

Querying the submit button names is a good idea. Here is a piece of code that works:

if ($this->input->post("submitlog") !== false) {
    //code for login part
} elseif ($this->input->post("submitreg") !== false) {
    //code for registration part
} else {
    //code for redirect
}

Upvotes: 2

Edwin Alex
Edwin Alex

Reputation: 5108

instead of that, you can keep a hidden field in both the forms with the value of corresponding form name. In the controller you can check this field value and identify which form is submitted. Then you can redirect into the required function..

Upvotes: 1

Related Questions