Welcome Guest, Not a member yet? Register   Sign In
Load view from Controller issue
#1

Hi there,

I am new to CodeIgniter framework and needs someone help, please.
I have an issue to redirect user back to Contact Us form after its submission.

My default controller is defined as below.

PHP Code:
<?php

class Pages extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
   }

 
   public function view($page 'home') {
 
       if (!file_exists(APPPATH 'views/pages/' $page '.php')) {
 
           show_404();
 
       }

 
       $data['title'] = ucfirst($page);

 
       $this->load->view('templates/header');
 
       $this->load->view('pages/' $page$data);
 
       $this->load->view('templates/footer');
 
   }



Now my view pages are in the sub-directory called pages, for example /views/pages/contact-us.php. So when I access this page like https://www.mysite.com/contact-us then it works fine and loads the contact us form as expected.

Look at my .htaccess file that removes index.php from the URL.

Code:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png|resources|robots\.txt)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

And also look at my routes.php file.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

$route['sendemail'] = 'sendemail/index';

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE

Now after user submits the contact us form it goes to controller called SendEmail.php and then I want user to come back to Contact Us form with message either successful or failed. I am getting an email to my gmail account but when it comes to loading VIEW it does not work.

Look at code of SendEmail.php

PHP Code:
<?php

defined
('BASEPATH') OR exit('No direct script access allowed');

class 
SendEmail extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
   }

 
   public function index() {
 
       // Check form submit or not
 
       //var_dump($this->input->post());

 
       if ($this->input->post('submit') != NULL) {

 
           // POST data
 
           $postData $this->input->post();

 
           //SMTP related configuration is defined in /config/email.php
 
           //it loads automatically by CodIgniter
 
           
            $this
->load->library('email');
 
           $this->email->set_newline("\r\n");

 
           $this->email->from($postData['email'], $postData['name']);
 
           $this->email->to(ADMIN_CURRENT_EMAIL);

 
           $this->email->subject(CONTACT_US_FORM_SUBJECT);
 
           $this->email->message('Testing the email class.');

 
           $data['is_error'] = false;

 
           if ($this->email->send()) {
 
               $data['message'] = 'The email has been sent successfully.';
 
           } else {
 
               $data['is_error'] = true;
 
               $data['message'] = 'Sorry, the email was not sent successfully. Try again later or send a direct email to ' ADMIN_CURRENT_EMAIL;
 
               //echo $this->email->print_debugger();
 
           }
 
           
            $this
->load->view('contact-us'$data);           
            
        
} else {
 
           echo "Invalid request.";
 
           die();
 
       }
 
   }

}

?>


I am getting below error. As I said above I manage to get email to my gmail account but loading a view does not work.

An Error Was Encountered
Unable to load the requested file: contact-us.php

Can you please help me?

Thanks - Hitesh
Reply


Messages In This Thread
Load view from Controller issue - by hiteshpatel1979 - 06-11-2018, 03:24 AM
RE: Load view from Controller issue - by InsiteFX - 06-12-2018, 03:11 AM
RE: Load view from Controller issue - by InsiteFX - 06-12-2018, 03:32 AM
RE: Load view from Controller issue - by InsiteFX - 06-12-2018, 06:42 AM
RE: Load view from Controller issue - by enelson - 06-12-2018, 09:17 AM
RE: Load view from Controller issue - by InsiteFX - 06-13-2018, 10:55 AM
RE: Load view from Controller issue - by InsiteFX - 06-15-2018, 06:36 AM
RE: Load view from Controller issue - by InsiteFX - 06-15-2018, 08:06 AM
RE: Load view from Controller issue - by InsiteFX - 06-15-2018, 09:33 AM
RE: Load view from Controller issue - by InsiteFX - 06-16-2018, 09:23 AM
RE: Load view from Controller issue - by InsiteFX - 06-22-2018, 10:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB