CodeIgniter Forums
Trouble returning to page after form submission - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Trouble returning to page after form submission (/thread-58309.html)

Pages: 1 2


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]Tekamba[/eluser]
I am building a website using CodeIgniter (my first) and so far I’ve worked through all the little issues except for this one.

I have a template setup for navigation in my primary controller. So calls to each page are formatted as such:
http://mywebsite.com/main/contact (for the contact us page. The function in main is formatted as such:

public function contact() {

$data[‘pagetitle’] = ‘Contact page for the website’;
$data[‘pageauthor’] = ‘Name of website author’;
$data[‘pagedesc’] = ‘Description of the page goes here’;
$data[‘media’] = ‘screen’;
$data[‘main_content’] = ‘contact’;
$this->load->view(‘includes/template’, $data);
}

The template file is this:
<?php
$this->load->view(‘includes/startpage’);
$this->load->view(‘includes/header’);
?>

<div id=“main_content”>
&lt;?php $this->load->view($main_content); ?&gt;
</div>

&lt;?php
$this->load->view(‘includes/footer’);
$this->load->view(‘includes/endpage’);
?&gt;

This works perfectly well through all my nav links and such, where it causes an error is when the user fills out the contact form and it gets submitted at the end of the email send function is this line:

$this->load->view(‘main/contact’);
This throws the error that the page cannot be found, but the page it is looking for is:
main/contact.php (not sure why it is adding the .php, it is correct if it weren't for that.

But the link that is generated is: http://mywebsite.com/email/contactsend (email is the class name of the email process functions, the email does send but the link doesn’t re-direct to the appropriate page and I am not sure what I have setup incorrectly.

Thanks for any guidance.

Greg


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]KristenBlackburn[/eluser]
try

$this->load->view('main/'.$main_content.'');


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]Tekamba[/eluser]
That didn't work, I am not sure what is causing the system to add the .php to the link. The error that shows up on screen from CI states:

An Error Was Encountered

Unable to load the requested file: main/contact.php

If it was just trying to simply load main/contact (without the .php) I think it would work perfectly. Why is it addding the php file extension?

Greg


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]Tekamba[/eluser]
Could my .htaccess file be causing the issue?

RewriteEngine on
RewriteCond $1 !^(index\.php|admin|css|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Those are the contents of .htaccess, I am using it to remove the index.php from my URL for a cleaner look.


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]pzntec[/eluser]
no the .htaccess file shouldn't have anything to do with it.. Why not just call your main controller after a successful submission?


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]Tekamba[/eluser]
Because anything I do with the $this->load->view(); function adds the .php to the end of it and thus fails to load. I can't seem to send the user back to any page on the site. Need to figure out why it is adding the file extension.


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]pzntec[/eluser]
try loading the controller or model instead. Depends on how you have things setup.

After which, it should be the one to call the view file.


Trouble returning to page after form submission - El Forum - 06-01-2013

[eluser]Tekamba[/eluser]
I tried $this->load->controller('main/contact'); but received the error:

Fatal error: Call to undefined method CI_Loader::controller() in /public_html/application/controllers/email.php on line 71

Is that not the correct way to load a controller?


Trouble returning to page after form submission - El Forum - 06-02-2013

[eluser]pzntec[/eluser]
It appears controller is not a method which is why you get that error.
try: using the header function: http://php.net/manual/en/function.header.php
Code:
header("Location: http://localhost/main/contact");
remember to change localhost to your domain if you are using one.

This might not be the best solution but i'm pretty sure it'll work.


Trouble returning to page after form submission - El Forum - 06-02-2013

[eluser]Tekamba[/eluser]
That is actually how I ended up resolving it, was kind of hoping there was a more elegant solution as this seems a bit barbaric, but could just be my still fairly limited PHP capabilities.

Here is the final solution:

I created a session variable to hold the message for the user (success/failure sending), I then created a new view file called confirm_send.php which simply contains:

&lt;?php
$baseurl = base_url();
$baseurl .= "main/contact";
header ( 'Location: ' . $baseurl);
?&gt;

And then changed my email controller file to $this->load->view('confirm_send');

I think my real problem may have been that I am using a template for my pages, so the email function was not calling the template properly and thus I wasn't getting a "full" page load, just the portion that should have been wrapped by the rest of my template calls. I also realize I hadn't mentioned the variable issue, but that is why I was attempting to make this work as I was hoping to just pass the variable across in the call, but I've now enabled sessions, which aren't really necessary for this site, but now I could monitor traffic to the site a bit on my own (outside of Google Analytics) should the client desire that in the future.

Thanks for all the tips, this was actually a pretty good challenge to help me get my head more into the CI framework, I'm really liking it, now just trying to tie in 960.gs and I think we'll be well on our way to some nicely capable websites with minimal fuss.

Greg