Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Problem with routing from a method to another controller
#3

(This post was last modified: 12-13-2014, 07:42 AM by g3n1u5.)

It's not working!


PHP Code:
$route['default_controller'] = 'pages/view';
//$route['movies/add'] = "movies/add
//$route['pages/view/addmovie'] = "movies/add"
$route['(:any)/addmovie'] = "movies/add";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = "pages/view/$1"

I tried all. Sorry if I am doing wrong or I did not understand. I am trying just to make the form to reload at the same page, and if there is an error the validation_error() is echoed if not some success message is outputted. 

The action attr is set to 
PHP Code:
<form action="http://localhost/ci/addmovie" class="col-sm-4" role="form" method="post" accept-charset="utf-8"


I managed to make it working by redirecting to another page ( as shown above - movies/add ) but I'd like to know if I can make it reload the same page. I don't want unnecessary reloads. 

P.S.

addmovie.php from the view
PHP Code:
   <!-- Main page content -->
 
       <div class="container-fluid">
 
       <div class="col-sm-4"></div>
 
       <?php
            $attr 
= array("class" => "col-sm-4""role" => "form");
 
           echo form_open($ADD_URL$attr); 
 
           echo validation_errors();
 
       ?>
              <div class="form-group">
                <input type="text" class="form-control" id="movieTitle" name="movieTitle" placeholder="Movie title" />
              </div>
              <div class="form-group">
                <input type="text" class="form-control" id="lastEpisodeWatch" name="lastEpisodeWatched" placeholder="Last episode watched" />
              </div>
              <div class="form-group">
                <input type="text" class="form-control" id="sourceURL" name="sourceURL" placeholder="Source url" />
              </div>
              <button type="submit" class="btn btn-default">Track me!</button>
            </form>
        </div>
    <!-- Main page content end --> 

movies.php - controller

PHP Code:
<?php 

class Movies extends CI_Controller {
 
   
    public 
function __construct() {
 
       parent::__construct();
 
       $this->load->model("movies_model");
 
   }
 
   
    public 
function add() {
 
       $this->load->helper(array('form''url'));
 
       $this->load->library('form_validation');
 
       
        $data
['title'] = "Add new movie/seris";
 
       
        $this
->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">''</div>');
 
       $this->form_validation->set_rules('movieTitle'"Movie Title""required");
 
       $this->form_validation->set_rules('lastEpisodeWatched'"Last episode watched""required|integer");
 
       $this->form_validation->set_rules("sourceURL""Source""required|valid_url|prep_url");
 
       
        $data
['BASEURL'] = base_url();              //http://localhost/ci/assets/
 
       $data['SITEURL'] = base_url();              // http://localhost/ci/index.php/
 
       $data['ASSETSURL'] = base_url().'assets/' // http://localhost/ci/assets/
 
       
        if
($this->form_validation->run() === false) {
 
           $this->load->view('tpl/header'$data);
 
           $this->load->view('pages/addmovie');
 
           $this->load->view('tpl/footer'$data);
 
       } else {
 
           $this->movies_model->add_movies();
 
       }
 
       
    
}
 
   


Pages.php - controller 

PHP Code:
<?php

class Pages extends CI_Controller {
 
   
    public 
function __construct() {
 
       
        parent
::__construct();
 
       $this->load->model('movies_model');
 
       
    
}
 
   
    public 
function view($page "home") {
 
       
        if
(!file_exists(APPPATH.'/views/pages/'.$page.'.php')) {
 
           show_404();
 
       }
 
       
        $data
['title'] = ucfirst($page);
 
       $data['BASEURL'] = base_url();              //http://localhost/ci/assets/
 
       $data['SITEURL'] = base_url();              // http://localhost/ci/index.php/
 
       $data['ASSETSURL'] = base_url().'assets/' // http://localhost/ci/assets/
 
       $data['ADD_URL'] = base_url().'addmovie';
 
       $data['movies'] = $this->movies_model->get_movies();
 
       
        $this
->load->view('tpl/header'$data);
 
       $this->load->view('pages/'.$page$data);
 
       $this->load->view('tpl/footer'$data);
 
   }
}

?>

Don't mind some unnecessary attributes and methods, its for learning purposes only Smile 

Best regards, 
g3n1u5!
Reply


Messages In This Thread
RE: Problem with routing from a method to another controller - by g3n1u5 - 12-13-2014, 07:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB