CodeIgniter Forums
Is it really Ajax what I am looking for ? Can you check this code? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Is it really Ajax what I am looking for ? Can you check this code? (/showthread.php?tid=53706)



Is it really Ajax what I am looking for ? Can you check this code? - El Forum - 08-04-2012

[eluser]ytsejam[/eluser]
Hello ,
I am a newbie developer. This is the first time I try to use Ajax. When you start the webpage, a loading animation starts and navigation and content part divides into left and right sides. When I click navigation links I only want content part to change.
MY_Controller.php:
Code:
function render_page($view) {
    if( ! $this->input->is_ajax_request() )
    {
      //do this to don't repeat in all controllers...
      $this->load->view('templates/header', $this->data);
      //menu_data must contain the structure of the menu...
      //you can populate it from database or helper
    }

    $this->load->view($view, $this->data);

    if( ! $this->input->is_ajax_request() )
    {
     $this->load->view('templates/menu');
     $this->load->view('templates/footer', $this->data);
    }

About controller
Code:
public function view($page = 'about')
        {
         $this->load->helper('text');
            $this->data['records']= $this->about_model->getAll();
            if ( ! file_exists('application/views/pages/'.$page.'.php'))
            {
                // Whoops, we don't have a page for that!
                show_404();
            }

            $data['title'] = ucfirst($page); // Capitalize the first letter


            $this->render_page('pages/'.$page,$data);


        }

Ajax.js file is inside head part of header.php. But still I need to change url part by the menu selection not the only about.php.

Code:
$("#sidebar-content ul li a").click( function(){

        $.ajax({
                    url: "<?php echo site_url('about'); ?>",
                    type: 'POST';
                    data: JSON,
                    success: function(response) {
      $('#content').html(response.msg);
                    }
                });
                });

        return false;
});

my menu is

Code:
<div id="sidebar-content">
<ul id="menu">
<li class="current"><a href="&lt;?php echo site_url('home'); ?&gt;">ANASAYFA</a></li>
<li><a href="&lt;?php echo site_url('about'); ?&gt;">HAKKIMIZDA</a></li>
.
.
.
</div>

How can I make ajax to work with every menu item (home/about)? What is your suggestion about to post partials to content div?



Is it really Ajax what I am looking for ? Can you check this code? - El Forum - 08-05-2012

[eluser]LuckyFella73[/eluser]
Your javascript could look like this:

Code:
$("#sidebar-content ul li a").click( function(e){
e.preventDefault();
  var target_url = $(this).attr('href');
        $.ajax({
   url: target_url,
// more code here

That way you can load different urls - like set in your anchors.


Is it really Ajax what I am looking for ? Can you check this code? - El Forum - 08-05-2012

[eluser]ytsejam[/eluser]
Code:
$("#sidebar-content ul li a").click( function(e){
e.preventDefault();
  var target_url = $(this).attr('href');
   $.ajax({
   url: target_url,
     type: 'POST',
     data: data,
     success: function(response) {
      $('#content').html(response.msg);
     }
    });
     return false;
    });

I have written this one but still no success.


Is it really Ajax what I am looking for ? Can you check this code? - El Forum - 08-05-2012

[eluser]skunkbad[/eluser]
Have you thought about what you will do when the site visitor clicks the back button? How about when they want to bookmark one of your "pages"? While ajax definitely serves a purpose in nearly every website, I think you are going to find that your approach to simple site navigation is going to come back to bite you. My opinion is that you should go normal HTTP requests.


Is it really Ajax what I am looking for ? Can you check this code? - El Forum - 08-05-2012

[eluser]ytsejam[/eluser]
That is why I am trying to ask about it. All I want to is when I click home.php I want to see home. and when I click about.php I want to see about. I want to get all responses from database. I guess The best way is to Json here right?



Is it really Ajax what I am looking for ? Can you check this code? - El Forum - 08-05-2012

[eluser]NeoArc[/eluser]
Use HTML 5 onpushstate() if you want to develop Single Page Applications using CI.
In older browsers every local link should load a complete page, and not an ajax one... (Hi IE 6).

You may want to use a template engine for your views.

Some references:
https://developer.mozilla.org/en-US/docs/DOM/window.onpopstate
https://github.com/balupton/history.js
https://github.com/balupton/history.js/wiki/Intelligent-State-Handling
http://stackoverflow.com/questions/3957000/what-codeigniter-template-library-is-best