4

I am using zend framework on windows. I want to implement ajax in my project first time. I searched for help and created a very simple ajax functionality.

IndexController.php

public function indexAction() {
}

public function oneAction() {
}

public function twoAction() {
}

index.phtml

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/AJAX.js"></script>

<a href='http://practice.dev/index/one' class='one'>One</a>
<a href='http://practice.dev/index/two' class='two'>Two</a>
<br /><br />
<div id="one">one.phtml content comes here</div>
<div id="two">two.phtml content comes here</div>

AJAX.js

jQuery(document).ready(function(){
    jQuery('.one').click(loadOne);
    jQuery('.two').click(loadTwo);
});

function loadOne(event) {

    event.preventDefault();

    jQuery.ajax({
        url: '/index/one',
        success: function( data ) {
                    jQuery('#one').html(data);
                    }
    });
}

function loadTwo(event) {

    event.preventDefault();

    jQuery.ajax({
        url: '/index/two',
        success: function( data ){
                    jQuery('#two').html(data);
                    }
    });
}

Above code is working and loading data of one.phtml and two.phtml in 'one' and 'two' DIVs respectively when its link is clicked. You can see that I have to create separate jquery function for each link and also have to add new class for each link tag.

What I want to do ?:

I want to use only one jquery function for all AJAX requests and don't want to hard code url and success attributes in that function.

When I add "AJAX" class to any link tag then it should load content using AJAX.

Thanks.

Naveed
  • 41,517
  • 32
  • 98
  • 131

4 Answers4

8

for simple loading of data in divs i would use the load method

HTML

<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/AJAX.js"></script>

<a href='http://practice.dev/index/one' class='ajax' rel="one">One</a>
<a href='http://practice.dev/index/two' class='ajax' rel="two">Two</a>
<br /><br />
<div id="one">one.phtml content comes here</div>
<div id="two">two.phtml content comes here</div>

JS

jQuery(document).ready(function(){
    jQuery('.ajax').click(function(event){
       event.preventDefault();

       var target = '#' + jQuery(this).attr('rel');
       var href = jQuery(this).attr('href');
       jQuery( target ).load( href );

      });
});

Use a single class to identify all links that should use ajax calls instead of their normal use. And add a rel attribute to those links that will contain the id of the container div..

Pointy
  • 405,095
  • 59
  • 585
  • 614
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • Is that correct usage of the `rel` attribute, or would it make more sense to use the `data-` HTML5 attributes? – Eric Jul 17 '10 at 17:13
  • @Eric, you have a point. The usage i do is not the semantically intended by the w3 standards.. the `data-` would be more appropriate.. *although it would be invalid in non html 5 validators..* – Gabriele Petrioli Jul 17 '10 at 17:16
  • @gaby another possibility would be to put something like "target:one" or "target:two" into the "class" value. Then the script could just strip out the "one" or "two". – Pointy Jul 17 '10 at 17:24
  • @pointy, would that be considered valid html, having `:` in class names ? (*also, thanks for the edit..*) – Gabriele Petrioli Jul 17 '10 at 17:41
  • I don't think there are many rules about what's in class names, but you could always use something else, like "ajax-", as the prefix. I've never heard of any problems caused by funny characters in class names. – Pointy Jul 17 '10 at 19:18
  • @Pointy, indeed .. i just wondered about the `:` .. but validators are fine with it. – Gabriele Petrioli Jul 17 '10 at 19:26
  • Thanks. Its working but one problem still exists. When I click any link, it is not changing url in browser's address bar. Anyway its good for me. I am learning AJAX step by step and its good step no.2 for me. My next step will be json and ajax combination. – Naveed Jul 18 '10 at 17:50
  • @Naveed, have a look at this SO question/answer as it addresses the issue and a potential pitfall (*scrolling*) [Modifying document.location.hash without page scrolling](http://stackoverflow.com/questions/1489624/modifying-document-location-hash-without-page-scrolling) – Gabriele Petrioli Jul 18 '10 at 18:45
1

Simple and Nice. No Jquery required. Check this out: Bjax

Usage:

<script src="bjax.min.js" type="text/javascript"></script>
<link href="bjax.min.css" rel="stylesheet" type="text/css" />

Finally, include this in the HEAD of your html:

$('a').bjax();

For more settings, checkout demo here: Bjax Demo

endur
  • 682
  • 11
  • 13
0

Maybe this:

function loadData(url, callback) {
    jQuery.ajax({url: url, success: callback});
}

loadData('/index/one', function( data ) {
    jQuery('#one').html(data);
})

loadData('/index/two', function( data ) {
    jQuery('#two').html(data);
})

To make this even more compact you could define the same callback for both and then have the handler decide which element the response data should be written to.

Roman
  • 10,309
  • 17
  • 66
  • 101
  • 1
    here #one and #two are also hardcoded. can we not also pass this? For example; array('one'=>one.phtml,'two'=>two.phtml); – Naveed Jul 17 '10 at 17:10
  • Like I mentioned in the end of my reply, your response should contain something by which the Ajax event handler can tell what element it needs to apply the content of response to. – Roman Jul 19 '10 at 15:24
0

Compact version:

$(function(){
    $('.one').click(loadOne);
    $('.two').click(loadTwo);
});

function loadOne(event) {
    event.preventDefault();
    $('#one').load('/index/one');
}

function loadTwo(event) {
    event.preventDefault();
    $('#two').load('/index/two');
}
Eric
  • 95,302
  • 53
  • 242
  • 374