• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How the use Ajax with Codeigniter?

#1
[eluser]ytsejam[/eluser]
Hello,
I am a newbie developer and I am writing a website with codeigniter. It is really fun about anything I coded. Now my problem is , I use MY_Controller.

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);
    }
  }

My home.php/about.php works without any problems. I want only <div id="content"> part to change when i click about or other links in the navigation menu.
How can i enable the ajax?
Thank you .

#2
[eluser]ytsejam[/eluser]
No answer for this question?

#3
[eluser]CroNiX[/eluser]
You use javascript to send ajax requests, not php. You can only use CI to check to see if the request was sent via ajax.

#4
[eluser]skunkbad[/eluser]
[quote author="ytsejam" date="1344075129"]How can i enable the ajax? ...[/quote]

Ajax is part of javascript. I use jQuery to handle ajax because even after using javascript for years, I still feel like an imbecile. You will normally want to use ajax to do a POST request, where the post data is validated and used to generate content that is passed back to the browser. That response can be in a few forms, and the most common is JSON. If you have any javascript already, you will probably need to show it so we know what you are doing client-side, or your chances of getting help are slim to none.

#5
[eluser]ytsejam[/eluser]
Where can i find a good tutorial or information about ajax?

#6
[eluser]skunkbad[/eluser]
[quote author="ytsejam" date="1344109401"]Where can i find a good tutorial or information about ajax?[/quote]

http://www.w3schools.com/ajax/default.asp

http://jquery.com

#7
[eluser]ytsejam[/eluser]
my header.php :
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Kırmızı Eğitim Merkezi&lt;/title&gt;
  &lt;meta name="description" content="Empire - XHTML Template" /&gt;
&lt;!-- CSS --&gt;
&lt;link href='&lt;?php echo base_url(); ?&gt;assets/fancybox/jquery.fancybox-1.3.4.css' rel="stylesheet" type="text/css" /&gt;
&lt;!-- JAVASCRIPTS --&gt;
[removed][removed]
[removed][removed]

My pages are home.php and about.php. Can you show me the example of ajax usage with only div id=content to change and where to put the script(before footer or body tag or somewhere else?).

#8
[eluser]skunkbad[/eluser]
You should be able to put your javascript before the closing head or body tag. In the case of simple ajax, the location won't matter. You are, however, going to have to do your own homework. Searching the internet for "ajax basics" will lead to thousands, if not hundreds of thousands of results. Javascript syntax is a lot like php, so you'll have a good time learning.

#9
[eluser]ytsejam[/eluser]
Can you edit my code?
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>
.
.

This is what i am trying to do :

Code:
$("#sidebar-content ul li a").click( function(){
        
        $.ajax({
     url: "&lt;?php echo site_url('about'); ?&gt;",
     type: 'POST';
     data: JSON,
     success: function(msg) {
      $('#content').body(msg);
     }
    });
    });

        return false;
});

I used ajax.js file in head part. I need to arrange url for every page or use ajax code in all of the pages?

#10
[eluser]skunkbad[/eluser]
You're probably going to have something like this:

Code:
$("#sidebar-content ul li a").click( function(){
        
        $.ajax({
     url: "&lt;?php echo site_url('about'); ?&gt;",
     type: 'POST';
     data: JSON,
     success: function(response) {
      $('#content').html(response.msg);
     }
    });
    });

        return false;
});


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.