4

I need to create a page that will load divs from an external page using Jquery and AJAX.

I have come across a few good tutorials, but they are all based on static content, my links and content are generated by PHP.

The main tutorial I am basing my code on is from:
http://yensdesign.com/2008/12/how-to-load-content-via-ajax-in-jquery/

The exact function i need is as follows:

  1. Main page contains a permanent div listing some links containing a parameter.
  2. Upon click, link passes parameter to external page.
  3. External page filters recordset against parameter and populates div with results.
  4. The new div contains a new set of links with new parameters.
  5. The external div is loaded underneath the main pages first div.
  6. Process can then be repeated creating a chain of divs under each other.
  7. The last div in the chain will then direct to a new page collating all the previously used querystrings.

I can handle all of the PHP work with populating the divs on the main and external pages.
It's the JQuery and AJAX part i'm struggling with.

$(document).ready(function(){
    var sections = $('a[id^=link_]'); // Link that passes parameter to external page
    var content = $('div[id^=content_]'); // Where external div is loaded to
    
    sections.click(function(){ 
        //load selected section
        switch(this.id){
            case "div01":
                content.load("external.php?param=1 #section_div01");
                break;
            case "div02":
                content.load("external.php?param=2 #section_div02");
                break;          
        }
});

The problem I am having is getting JQuery to pass the dynamically generated parameters to the external page and then retrieve the new div.
I can currently only do this with static links (As above).

Community
  • 1
  • 1
ticallian
  • 1,631
  • 7
  • 24
  • 34

6 Answers6

6

I'm not sure if you've solved this already, but I'm surprised no one's mentioned to use the ajax() function.

This would allow you to define the request type as GET:

function loadContent(id) {

    $.ajax({
        type: "GET",
        url: "external.php",
        dataType: 'html',
        data: {param: id},

        success: function(html){
                 $("#container").html(html);
        },

        error: function(){
        },

        complete: function(){
        }
    });

}

Just call this function instead of using load. Obviously you'll have to tinker with the code (mainly what goes in the success function) a little, but this should give you a good starting point.

Scotty
  • 2,635
  • 6
  • 30
  • 39
  • For anyone in future, the above method is what I actually used in the end, although I never got around to updating this post, so here goes. – ticallian Sep 07 '09 at 06:17
2

You can use the optional data argument to pass parameters to the GET request. Read the documentation. This is far better than building the URL yourself. You can of course add dynamic generated data to the parameters list.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
0

This tutorial on loading AJAX content is good:

http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

Especially the part explaining how to read the results with Firebug.

waraker
  • 1,331
  • 11
  • 14
0

Use this :

function GetDiv(id) {

$.ajax({
    type: "GET",
    url: "external.php"
    dataType: 'html',
    data:id,   
    success: function(html){
             $("#container").append(html);
    },


});

}
AdagioDev
  • 556
  • 5
  • 12
0
function loadDiv(evt)
{
    // these params will be accessible in php-script as $_POST['varname'];
    var params = {name:'myDiv', var1:123, var2:'qwer'};
    $.post('http://host/divscript.php', params, onLoadDiv);
}
function onLoadDiv(data)
{
   $('#myContainer').html(data);
}
$(document).ready(function() { 
    $('#divButton').click(loadDiv); 
});

In this example server-side script should return inner content of your div. Sure you can return XML-serialized data or JS to eval etc... it depends on task. The example is simplified, so extend it to fit your needs.

Jet
  • 1,171
  • 6
  • 8
  • Thanks all for the pointers, i'll have a little play and see what i can rustle together. – ticallian May 28 '09 at 08:37
  • Ask for more if this example is too abstract. I can explain it in details, but I think that the common idea is enough to start playing around it. – Jet May 28 '09 at 09:26
  • I've given the solution a go, but the way I've implemented it causes the original div to be replaced with the whole content of the external page. I need the new div to be added underneath in a chain effect of divs. – ticallian May 28 '09 at 10:27
  • then use http://docs.jquery.com/Manipulation/append#content instead of .html(). I've just written the common solution. In onLoadDiv() you may apply more complex magics instead of just assigning inner content of container. But I can't provide you the complete solution because I don't have the entire page where it happens :). – Jet May 29 '09 at 10:18
-1
var params = {
    param: 1,
    otherParam: 2
};
content.load("external.php #section_div01", params);

will load "external.php?param=1&otherParam=2"

gregers
  • 12,523
  • 8
  • 46
  • 41
  • Calling load() with data arguments performs a POST request, not a GET. You would have to build that URL manually. – Chris Zwiryk Aug 14 '09 at 13:58
  • Where in the question does it state that it has to be a GET request? Jet's answer also includes a POST suggestion, and the original poster didn't seem to have any problems with that. – gregers Aug 21 '09 at 07:38