Welcome Guest, Not a member yet? Register   Sign In
Multiple Forms on Same Page
#1

Hello All.

I have joined this forum a few minutes after I downloaded and installed CI 3.0.6. And this is the first time I am using anything like CI. Though I have been working on Core PHP and Wordpress, I have never used any MVC before and I am quite a few hours into it only.

So here is my problem -

I have been asked to implement a website for my newly joined job and lets start with the home page first -

The home page has 3 Forms -
a. Login form - via the Main Menu
b. Subscribe form and
c. Signup form
(b and c on the lower end of the page).

Gladly, the help on Internet did help me integrate Bootstrap into Code Igniter and also the documentation helped me get started. But now, I am stuck up with these forms.

Code Snippets-

The Home Controller Class
PHP Code:
class Home extends CI_Controller{
 
   function __construct() {
 
       parent::__construct();
 
       $this->load->helper('form');
 
       $this->load->library('form_validation');
 
       $this->load->helper('url');
 
   }
 
   public function index()
 
   {
 
       if(!file_exists('application/views/pages/home.php')){
 
           echo "Sorry, The Home Page Does Not Exist";
 
       }
 
       else{
 
           //Load the home page view here
 
           $this->load->view('pages/home');
 
       }
 
   }

 
   public function subscribe(){
 
       $this->form_validation->set_rules('sname''Name''trim|required|min_length[5]');
 
       $this->form_validation->set_rules('semail''Email''trim|required|valid_email');
 
       if ($this->form_validation->run() === FALSE)
 
       {
 
           $this->load->view('pages/home');
 
       }
 
       else{
 
           echo "All Good... Try Storing data and emailing";
 
       }
 
   }

 
   public function signup(){
 
   }

 
   public function dologin(){
 
   }



The Home View -
This has the Nav Bar (with a dropdown Login Form) and then there are two forms - Subscribe and Signup

Now, the problem that crops up is -
When I submit the Subscribe form with an intentional error (that violates the rules), the error messages are displayed on all 3 forms.

I did read on Internet that it is not advisable to have 3 forms in one view and also, if there is no way out, then, I should use Hidden fileds to identify which form has been submitted.

But somehow, as I am quite very new here, I would love to read what the Experienced Community on this forum thinks. What are the best practices in such situations.

Thanks in advance for your patience to read through this and helping me become proficient in using CI.

Warm regards,
Kevin.
Reply
#2

You can have more then one form on a page. Your form field names should be unique for each form. You can display the errors specific to the exact field - study this:
http://www.codeigniter.com/user_guide/li...dividually
Reply
#3

(07-28-2016, 11:19 AM)cartalot Wrote: You can have more then one form on a page. Your form field names should be unique for each form. You can display the errors specific to the exact field - study this:
http://www.codeigniter.com/user_guide/li...dividually

Thanks for this. I might not have found this segment of the document that easily.

Now, if I do this, do you think it would be a better idea to load the form validation library locally in the function to validate - for instance, the subscribe() or the dologin()? At the moment, I think because I am loading the Helper and also the Library in the constructor, the error messages are displayed for all existing forms.

Thanks again for the URL. I will be studying this in detail tonight.
Reply
#4

Where you load the library/helper doesn't determine where the error messages are displayed. The error messages are displayed wherever you happen to call the function(s) to display them.

If you use the validation_errors() function, it's going to spit out whatever errors are there. So, in this case, you either want to display it in a prominent location on the page which isn't necessarily associated with a specific form, or not use the function at all. Then you use the form_error() function to display the errors on a per-field basis within the form(s), but this will require that the names of the fields in the forms be unique (which isn't a requirement, otherwise).

If you pass a variable back to the page to indicate which form was processed, you could check that before calling validation_errors() on each form (and the form_error() function if you duplicate your field names) to prevent the errors from being displayed on the wrong form(s). I also use the form_error() function to set an error class on my fields so I can use CSS to draw attention to them, like this:

PHP Code:
<div class='control-group<?= form_error("username") ? " {$errorClass}" : ""; ?>'>
    <
label for='username'><?= lang('username'); ?></label>
    <input type='text' name='username' value="<?= set_value('username', isset($record['username']) ? $record['username'] : ''); ?>" />
</div> 
Reply
#5

(07-28-2016, 12:10 PM)Petersk Wrote: Now, if I do this, do you think it would be a better idea to load the form validation library locally in the function to validate - for instance, the subscribe() or the dologin()?

If the form validation fails the best experience for your user is to just show the form they are trying to fill out with the error message next to the relevant field so its as clear as possible.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB