Naren Verma
Naren Verma

Reputation: 2307

Controller function runs every time when page is refreshed using codeigniter

I have a single button. When I clicked on the button then it's called the function demo inside my testcontroller.I have to call the view page called as createsection also I have to call the model(For sending an email which is working).

After clicking on the button now I am on createsection page but when I fresh the page It's again calling demo function in the controller.

I just need on a single click to call view and also call the model in the background. User will get the view page and model can send the email.

welcome.php

<?php echo form_open('testcontroller/demo'); ?>
<button name="clicked">click me</button>
<?php echo form_close(); ?>

testcontroller/demo

class testcontroller extends CI_Controller {    
public function demo(){
    $email=$this->session->userdata('email');

    $this->load->view('createsection');

    $this->load->model('user_model');
    $id=$this->user_model->new_user($email);//passing email id in model to send the email

    //more code here..............
}
}

createsection (view page)

The user will get this page after clicking on the button.

   <?php echo form_open('testcontroller/confirm'); ?>
    <input type="text" name="code">
    <input type="submit" name="submit">
    <?php echo form_close(); ?>

Upvotes: 1

Views: 1370

Answers (3)

ubm
ubm

Reputation: 636

I think, when you are refreshing the page newuser model function executing multiple times. You can avoid these problem by redirecting

class testcontroller extends CI_Controller {

    public function demo(){
        $email = $this->session->userdata('email');

        $this->load->model('user_model');
        // passing email id in model to send the email
        $id=$this->user_model->new_user($email); 

        redirect('testcontroller/demo_view/');
        //more code here..............
    }

    public function demo_view(){
        $this->load->view('createsection');
    }
}

Upvotes: 2

Xpleria
Xpleria

Reputation: 5853

Simple answer to your question is:

A function inside a Controller is not invoked on UI events but when the page is loaded.

For example, if you load the page testcontroller/demo, the demo() function is executed. If you load the page testcontroller/confirm, the confirm() function is executed. Of course, these functions should exist otherwise you would get a 404 not found error.

Upvotes: 0

Gss Venkatesh
Gss Venkatesh

Reputation: 170

Hey Make this change in you controller and run the testcontroller it will open the welcome.php page Then you can find the confirm form Click on the submit button it will work

 class testcontroller extends CI_Controller {    


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

        public function demo() {
            $this->load->view('createsection');
        }
        public function confirm(){

            $email=$this->session->userdata('email');

            $this->load->model('user_model');
            $return = $this->user_model->new_user($email);         
        }
     }

Upvotes: 2

Related Questions