How to call controller function from view in codeigniter?

后端 未结 4 1508
陌清茗
陌清茗 2021-01-16 20:17

With codeigniter, I have a controller as in the following:



        
相关标签:
4条回答
  • 2021-01-16 20:29

    if You Want to call controller function like this /CodeIgniter/myController/myFn then, You have to remove index.php file using .htaccess file. First You change config.php file like this.

    //  Remove index.php
    
    $config['index_page'] = ""
    

    then, create .htaccess file and copy this code to that file.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    

    Then, put this .htaccess file inside codeignator project folder.

    To Reference see this link:- enter link description here

    0 讨论(0)
  • 2021-01-16 20:30

    In your controller just delete ' ** '.

    <?php if(!defined ('BASEPATH')) exit('not found basepath');
    
    class myController extends CI_Controller{
    
        function __constructor(){
            parent::__constructor();
        }
    
        public function index(){
            $this->load->view('myview');
        }
    
        public function myFn(){
        echo "my controller is called"; 
        }
    }
    ?>
    

    And In your view try:

    <?php echo form_open('myController/myFn'); ?>
    <?php echo form_submit('submit','SUBMIT'); ?>
    
    0 讨论(0)
  • 2021-01-16 20:33

    when you run the view

    <form action="<?php echo base_url();?>myController/myFn" method="post" name="myform">
    <input type="submit" name="submit" value="submit"/>
    </form>
    

    use inspect element to see if the action in the form tag is same as

    **http://localhost/CodeIgniter/index.php/myController/myFn**
    
    0 讨论(0)
  • 2021-01-16 20:37

    I think you forgot to load URL helper

    function test()
    {
    
        $this->load->helper('url');
        $data['message']= $this->input->post('message');
        $this->load->view('backend/test',$data);
    
    }
    

    this might help you

    0 讨论(0)
提交回复
热议问题