[eluser]_Darkdreams[/eluser]
Hi all, please excuse the junior nature of this request for help but after days of searching I have drawn a blank.
I have followed lots of CI tutorials and am building a site around a simple set of templates.
template_view.php contains the following:
Code: <?php $this->load->view('includes/header_include'); ?>
<?php $this->load->view('includes/navigation_include'); ?>
<?php $this->load->view($mainContent); ?>
<?php $this->load->view($sideContent); ?>
<?php $this->load->view('includes/footer_include'); ?>
with $mainContent and $sideContent being defined in the controller.
Now in a typical controller I use $data as an array to hold all sorts of information to pass to each page such as meta data and the page title and a marker for the nav to know what page is what.
Now what I want to do when submitting my form is reload the page of the validation fails and change just the $mainContent value to the success template if the form submits correctly.
Here is my controller at the moment:
Code: <?php
class Contactme extends Controller {
// index function
function index()
{
$data = array(
'mainContent' => 'main_view', // set mainContent to the view we want to see as the main content
'sideContent' => 'main_secondary_view', //set sideContent to the view we want as the side bar
'meta' => array( //set the meta data for the page
array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'),
array('name' => 'description', 'content' => 'My Great Site'),
array('name' => 'keywords', 'content' => 'keywords go here'),
array('name' => 'language', 'content' => 'English'),
array('name' => 'author', 'content' => 'Author goes here'),
array('name' => 'copyright', 'content' => 'Copyright goes here'),
array('name' => 'revisit-after', 'content' => '30 days'),
array('name' => 'document-class', 'content' => 'Completed'),
array('name' => 'document-classification', 'content' => 'Design'),
array('name' => 'document-rights', 'content' => 'Copyrighted Work'),
array('name' => 'document-type', 'content' => 'Public'),
array('name' => 'document-rating', 'content' => 'General'),
array('name' => 'document-distribution', 'content' => 'Global'),
array('name' => 'document-state', 'content' => 'Dynamic'),
array('name' => 'cache-control', 'content' => 'Public'),
array('name' => 'robots', 'content' => 'FOLLOW')
),
'pageTitle' => 'This is the page title', //set the page title
'thisPage' => 'contactMe' //set the navigation element to have the selected tab
);
$this->load->view('includes/template_view' , $data); //load the page
}
function send () {
$this->load->library('form_validation');
//validation rules
//field name, validation message, validation rules
$this->form_validation->set_rules('forename', 'Forename', 'trim|required');
$this->form_validation->set_rules('surname', 'Surname', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('comments', 'Comments', 'trim|required');
if ($this->form_validation->run() == FALSE) {
//validation has failed, load the form view page again
$this->load->view('contact_view');
}
else {
//validation has passed now send the email
//first save each form field as a variable
$forename = $this->input->post('forename');
$surname = $this->input->post('surname');
$email = $this->input->post('email');
$comments = $this->input->post('comments');
$this->load->library('email');
$this->email->set_newline("\r\n");
$this->email->from($email);
$this->email->to('[email protected]');
$this->email->subject('Contact from the website');
$this->email->message($comments);
if ($this->email->send())
{
$this->load->view('contact_success_view');
//echo 'your email was sent';
//need to create a solution to send the user back to the form page and load a sucess message
}
else
{
show_error($this->email->print_debugger());
}
}
}
}
More to come, i've run out of room
[eluser]_Darkdreams[/eluser]
Where was I...
Now here comes the bit where I am stuck. If the form validation fails, do I say
Code: $this->load->function('index');
or
Code: $this->load->view('includes/template_view' , $data);
to reload the page?
The other bit that has me stumped is what to do to change the $mainContent value to load the contact_success_view.php file?
As the $data array is in the index function it would seem that $data is not available to the functions in the rest of the controller.
I appreciate that this is probably basic stuff but I am trying to teach this stuff to myself and this time I am truly stuck. Could someone point me in the right direction or tell me what I am doing wrong so I can move closer to figuring this out and making progress with CI and php?
Regards
Jamie
[eluser]dmorin[/eluser]
One option would be to make your data array a class variable like so:
Code: <?php
class Contactme extends Controller {
var $data = array(
'mainContent' => 'main_view', // set mainContent to the view we want to see as the main content
'sideContent' => 'main_secondary_view', //set sideContent to the view we want as the side bar
'meta' => array( //set the meta data for the page
array('name' => 'Content-type', 'content' => 'text/html; charset=utf-8', 'type' => 'equiv'),
array('name' => 'description', 'content' => 'My Great Site'),
array('name' => 'keywords', 'content' => 'keywords go here'),
array('name' => 'language', 'content' => 'English'),
array('name' => 'author', 'content' => 'Author goes here'),
array('name' => 'copyright', 'content' => 'Copyright goes here'),
array('name' => 'revisit-after', 'content' => '30 days'),
array('name' => 'document-class', 'content' => 'Completed'),
array('name' => 'document-classification', 'content' => 'Design'),
array('name' => 'document-rights', 'content' => 'Copyrighted Work'),
array('name' => 'document-type', 'content' => 'Public'),
array('name' => 'document-rating', 'content' => 'General'),
array('name' => 'document-distribution', 'content' => 'Global'),
array('name' => 'document-state', 'content' => 'Dynamic'),
array('name' => 'cache-control', 'content' => 'Public'),
array('name' => 'robots', 'content' => 'FOLLOW')
),
'pageTitle' => 'This is the page title', //set the page title
'thisPage' => 'contactMe' //set the navigation element to have the selected tab
);
// index function
function index()
{
$this->load->view('includes/template_view' , $this->data); //load the page
}
...
So then you can use it from any mention within the class. Also, if you want to call another method from within the same class, just use $this->index() instead of $this->load->function('index');
[eluser]_Darkdreams[/eluser]
Thank you
A solution quite like me.... so simple
I figured that the solution was something like you suggested but for the life of me I kept getting close but not getting it right.
Thanks again,
Jamie
[eluser]_Darkdreams[/eluser]
At the moment my template works fine although I can see many advantages when I expand the site to include user information and session data
Ok so I am slightly stuck again, so my array contains this value:
Code: $data = array(
'mainContent' => 'contact_main_view',
which defines the view I want to load for the page. Now on the contact form I need to change this value to the new view if the form is submitted successfully.
Code: $data = array(
'mainContent' => 'contact_success_view',
I have this at the moment..
Code: if ($this->email->send())
{
$this->load->view('includes/template_view' , $this->data);
}
How do I assign the new value for mainContent?
Code: $data[mainContent] = "contact_success_view";
doesn't seem to work if I add it to the if statement above, before the $this->load line.
Any clues? I appreciate this is basic stuff but I am doing my best to learn as I go
Jamie
[eluser]_Darkdreams[/eluser]
Yay figured this out,
Code: $this->data['mainContent'] = "contact_success_view";
I missed the '' around mainContent.
Thanks for the pointers and help with this
Jamie
|