Welcome Guest, Not a member yet? Register   Sign In
Update Main Content of Template when using Ajax
#1

[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">
&lt;?php $this->load->view($main_content); ?&gt;
</div>

&lt;?php $this->load->view('layout/footer'); ?&gt;
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).
#2

[eluser]InsiteFX[/eluser]
Replace $ in script tag with s
Code:
&lt;head&gt;
<$cript type="text/javascript" charset="utf-8">
  //&lt;![CDATA[
   var base_url = "&lt;?php echo base_url(); ?&gt;";
  // ]]>
</$cript>
&lt;/head&gt;

Code:
$("#add-faq").submit(function(e){

e.preventDefault();
var dataString = $("#add-faq").serialize() + "&ajax=1";

$.ajax({
  type: "POST",
  url: base_url+"index.php/faqs/save_faq",
  data: dataString,

  success: function(data){
   $('#main_content').html(data);
   $('.success-notice').html('<p>FAQ Added</p>');
  }

});

return false;

});
#3

[eluser]Unknown[/eluser]
I am not entirely sure how your solution of adding my base_url (and I have removed index.php so don't need that) helps with my problem of reloading the main content of my template? Everything works exactly as it should otherwise (i.e. the Ajax processes the PHP and if I just return a string that is fine) so I haven't got a problem with the url in the ajax.
#4

[eluser]GrahamDj28[/eluser]
You must echo your data that you want to return. If you don't do that then it will have no data to add to the content.

Try this:
Code:
if ($this->input->post('ajax')) {
    // How do I reload the $main_content from admin_index function here??
    echo json_encode($data);
    exit;
} else {
    redirect('faqs/admin_index');
}

By doing this you must decode the json in your ajax success method.




Theme © iAndrew 2016 - Forum software by © MyBB