[eluser]Unknown[/eluser]
I am fairly new to CI and have come across an issue that I just can't seem to resolve or find an answer for. I have an admin page that lists all my FAQ's and on that page I can add, edit and delete FAQs which all works fine using CI and Ajax (in general).
The problem I am having is that I want to 'reload' the main content of my CI template after adding an FAQ so that the new FAQ is listed and I just can't get it to work.
In my main template I have:
Code:
<?php $this->load->view('layout/header'); ?>
<?php $this->load->view('layout/sidebar'); ?>
<div id="main_content">
<?php $this->load->view($main_content); ?>
</div>
<?php $this->load->view('layout/footer'); ?>
The view is in
views/owner-admin/admin/faqs
The Ajax
Code:
$("#add-faq").submit(function(e){
e.preventDefault();
var dataString = $("#add-faq").serialize() + "&ajax=1";
$.ajax({
type: "POST",
url: "/faqs/save_faq",
data: dataString,
success: function(data){
$('#main_content').html(data);
$('.success-notice').html('<p>FAQ Added</p>');
}
});
return false;
});
In the controller the main method is
Code:
function admin_index()
{
if($query = $this->core_model->get_records('faq_categories')) {
$this->data['categories'] = $query;
}
if($query = $this->core_model->get_records('faqs')) {
$this->data['faqs'] = $query;
}
$this->data['page_title'] = 'Admin FAQs';
// Load the owner-admin/admin/faqs View in the $main_content placeholder in the template
$this->data['main_content'] = 'owner-admin/admin/faqs';
// Load the template and pass it the data array
$this->load->view('layout/template', $this->data);
}
And for saving an FAQ
Code:
function save_faq()
{
$id = $this->input->post('id');
if ($_POST['slug'] == '') {
$_POST['slug'] = str_replace(' ', '-', strtolower($_POST['question']));
}
$data = array(
'category_id' => $this->input->post('category_id'),
'question' => $this->input->post('question'),
'answer' => $this->input->post('answer'),
'slug' => $this->input->post('slug')
);
if (!empty($id)) {
$query = $this->core_model->update_record('faqs', $id, $data);
} else {
$query = $this->core_model->add_record('faqs', $data);
}
if ($query) {
if ($this->input->post('ajax')) {
// How do I reload the $main_content from admin_index function here??
} else {
redirect('faqs/admin_index');
}
} else {
echo '<p>There was an error in adding your FAQ. Last Query:</p>'.$this->db->last_query();
}
}
I just cannot work out how to reload the list of FAQs after the update without having to reload the whole page (how do I reload the $main_content from the admin_index function).