SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning data from ajax to new element

    I'm having trouble placing some data returned by an ajax function in the page where I want it.

    I've included the relevant html and the javascript function below. There is some php code included which works but is not relevant to this question. The first ajax part of the function works. I have tested it with positive results in another context.

    The part starting with "var x . . ." is the part that is not working. This part is supposed to generate the following html:

    <div id="1">{content returned by ajax function}</div>

    inside of the <div id="circ_1">

    It doesn't.

    Help!

    Here is the html that calls the function:

    <div id="circ_1">
    <table cellspacing="0">
    <tr>
    <td class="bg" height="100" align="center" valign="middle" onMouseOver="showHidePage('circ_1', 'page-1')" ><? if (!empty($row['title_short'])) echo $row['title_short']; ?></td>
    </tr>
    </table>
    </div>

    Here's the function:

    function showHidePage(objID, page) {

    //Set remote php location.
    var serverPage = 'pages/' + page + '.php';

    //Assign the container div to a variable
    var obj = document.getElementById(objID);

    //Implement ajax request.
    xmlhttp.open("GET", serverPage);
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

    //Create new html objects and include ajax results.
    var x = document.createElement('div');
    x.setAttribute("id","1");
    var text = xmlhttp.responseText;
    var y = document.createTextNode(text);
    x.appendChild(y);

    //Append to container object
    obj.appendChild(x);

    }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Have you checked that you're actually receiving the text? Try adding this:
    Code:
    alert(text);
    before this:
    Code:
     var y = document.createTextNode(text);
    And next time you post code, please highlight it properly using the choices in Advanced mode, or by just doing [highlight="Javascript"] {js here} [/highlight].

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks for the tips

    Raffles -- Thanks for the debugging tip. This is part of javascript I am just learning and it is quite different than php debugging. I will try it.

    I will take some time next time to learn how to use the highlighting features you mention. This is my first post here. Just signed up and just getting into javascript and this forum.

    I have some additional tips from other sources, as well and will see what I can get together.

    Thanks,

    --Kenoli

    Quote Originally Posted by Raffles View Post
    Have you checked that you're actually receiving the text? Try adding this:
    Code:
    alert(text);
    before this:
    Code:
     var y = document.createTextNode(text);
    And next time you post code, please highlight it properly using the choices in Advanced mode, or by just doing [highlight="Javascript"] {js here} [/highlight].


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •