0
votes

I would like to include a CodeIgniter comment form in an existing php page. Currently I am including the comment form with

$_SERVER["REQUEST_URI"] = "comment/index2/".$page;
                require_once($directoryLevelPrefix . "staff/codeigniter/index.php");

However, when codeigniter loads the view to display the results of the form I lose the content on my original php page which contained the form. I have tried adding a redirect after the form data is added to the database to redirect to my original php page e.g.

redirect($_POST['return'])

But I want to pass some variables, such as 'thank you for your comment', and this causes a headers already sent error.

Alternatives might be to use ajax, but I would like the whole page to refresh when the comment is submitted.

2

2 Answers

0
votes

I had a similar need and I just loaded the CodeIgniter form in an iframe. This was to put a contact form on a WordPress page. I went with the iframe because my needs were limited to this one form and I felt it didn't require trying to integrate CodeIgniter and WordPress in any way.

One Pro of this method is that the form can be submitted and load a confirmation page, an error page, or even be a multi-page form and all that navigation takes place inside the iframe so you never leave the page that you are on while using the form. Another Pro is that the form is totally portable.

The Cons of course are anything you might not like about using iframes which could be many things based on your needs and preferences.

0
votes

That is because CodeIgniter is a framework designed to handle displaying entire web pages. You can't just include it, as it tries to send new headers and everything.

Two solutions:

  • You may be able to load the codeigniter page as the main page, and just include your original php code like a view file. This depends on how your original php file is set up.

  • You could possibly use cURL to call the page from your own server, to get the html output as a string, then echo it to your current page. This isn't very efficient though.

  • As C. Scott Asbach mentioned, you could load the whole page in an iframe. Probably not a good idea though, as far as I'm aware iframes are deprecated and make for messy websites.

Probably best is to just try and avoid using CodeIgniter to display a subform in an existing page :-)