2017-09-27 145 views
0

我有一个按钮。当我点击按钮时,它被称为testcontroller中的函数演示。我必须调用视图页,调用createsection也必须调用模型(用于发送正在工作的电子邮件)。控制器功能在每次页面刷新时使用codeigniter

点击按钮后,我现在在createsection页面,但当我刷新页面它再次在控制器中调用演示功能。

我只需要一次点击就可以调用视图,并在后台调用模型。用户将获得查看页面,模型可以发送电子邮件。

的welcome.php

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

的TestController /演示

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(查看页)

用户将点击该按钮后,在该网页。

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

无论何时载入'testcontroller/demo'页面,'demo'函数都会被执行。你为什么要刷新页面?你想要实现什么功能? –

+0

因为你有加载视图页面,而不是刷新后更改控制器你仍然在同一个控制器,这是为什么再次调用相同的控制器。 –

+0

@NeilPatrao,In view(createsection)页面我正在接收用户通过电子邮件获取的确认码。如果我刷新页面,则电子邮件将继续。 –

回答

1

我想想,当你刷新页面newuser模型函数执行多次。您可以通过重定向

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'); 
    } 
} 
+0

感谢您的回复Mr.ubm,您的代码正在为我完美地工作,并感谢您理解我的问题。从我身边高歌猛进。 –

1

嘿让你控制这种变化和运行的TestController 它会打开页面的welcome.php然后你就可以找到确认表单点击提交按钮,将工作

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);   
     } 
    } 
+0

点击提交按钮后,我不得不打电话确认功能吗? –

+0

是啊..检查现在..它会工作 –

+0

对不起,迟到的答复,先生,谢谢您的答复。以上的解决方案是为我工作.upvote从我身边。 –

0

简单回答你的问题避免这些问题是:

一个Controller内部的功能则不会调用在UI事件,但是当加载页面。

例如,如果您加载页面testcontroller/demo,则会执行demo()函数。如果您加载页面testcontroller/confirm,则会执行confirm()函数。当然,这些函数应该存在,否则你会得到404找不到错误。

相关问题