Ajax html include help

Discussion in 'PHP' started by paul_so40, Sep 17, 2011.

  1. #1
    Hey all,


    Im having problems using ajax. This is what im trying to achieve - put simply,

    The html contents of a div tag to be replaced with the html of a file called page1.html - after clicking a text link.

    Could anyone give me example of how to do this? i have tried things online and just got confused. would appreciate an example that ill use as a template.

    Thanks

    Paul
     
    paul_so40, Sep 17, 2011 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    try this out:

    <script language="javascript">

    function get_page(target,url){

    var http = new XMLHttpRequest();
    http.open("GET", url, true);

    http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
    document.getElementById(target).innerHTML = http.responseText;
    }
    }
    }
    </script>



    usage:


    <div id="content">

    </div>

    <a href="" onclick="get_page('content','page1.html');">Page</a>
     
    Last edited: Sep 17, 2011
    shofstetter, Sep 17, 2011 IP
  3. paul_so40

    paul_so40 Peon

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Thanks for the reply but this doesn't work for me.
     
    paul_so40, Sep 17, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    The PHP page (the "url" in http.open("GET", url, true);) has to echo the HTML you want inserted into the div.
     
    Rukbat, Sep 17, 2011 IP