www.webdeveloper.com
Results 1 to 10 of 10

Thread: Ajax Poll - How to retrieve new score to the correct span id

  1. #1
    Join Date
    Jan 2009
    Posts
    5

    Unhappy Ajax Poll - How to retrieve new score to the correct span id

    I want to create an AJAX poll to rate comments. In each comment you can either thumbs up, or thumbs down by clicking on an image. I achieved that part. However, I don't know how to update the score with AJAX in the correct span.

    if(xmlHttp.readyState ==4)
    {
    document.getElementById ("response").innerHTML =xmlHttp.responseText;
    }

    That shows the result in <span id="response">new score</span>

    I want the new score to be updated next to the thumbs up or thumbs down image, in the comment that was rated.

    I generate the span tags like this:

    <span id="response<?php echo $comment_id; ?>">I want the new score here</span>

  2. #2
    Join Date
    Feb 2008
    Posts
    1,666
    If you don't know what the value of $comment_id is ahead of time, then the onclick event will have to pass this id to the AJAX code so that the AJAX code can send back the returned score to the proper place.

  3. #3
    Join Date
    Jan 2009
    Posts
    5
    I don't know how to pass the id to AJAX with the onclick event. I'm not very good with JS.

  4. #4
    Join Date
    Mar 2007
    Posts
    946
    onclick="function_name(parameter)"

  5. #5
    Join Date
    Feb 2008
    Posts
    1,666
    Quote Originally Posted by yspa View Post
    I don't know how to pass the id to AJAX with the onclick event. I'm not very good with JS.
    Show the onclick event you're using now to invoke the AJAX process. Also show the HTML around where the onclick event is so that I can see the relationship between where the onclick is and where the response container is.

  6. #6
    Join Date
    Jan 2009
    Posts
    5
    <input type="image" id="heart" src="images/heart.png" onClick="ajaxFunction();" value="<?php echo $comment_id; ?>&mode=voteup" />

    <span id="response<?php echo $comment_id; ?>



    var myQuery = document.getElementById('heart');

    var url="vote.php"
    url=url+"?q="+myQuery.value
    url=url+"&sid="+Math.random()
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

  7. #7
    Join Date
    Feb 2008
    Posts
    1,666
    OK, since $comment_id is also placed in the value for the INPUT element, then you can code your inline event attribute as follows to pass that to your function as an argument:
    Code:
    onclick="ajaxFunction(this.value)"
    You'll just need to extract it for use in the getElementById method to obtain addressability to the response span.

  8. #8
    Join Date
    Jan 2009
    Posts
    5
    and to extract it I would use one of the methods, such as split(delimiter), described in this page?
    http://www.javascriptkit.com/javatutors/string4.shtml

  9. #9
    Join Date
    Feb 2008
    Posts
    1,666
    Yep, that be one way.

  10. #10
    Join Date
    Jan 2009
    Posts
    5
    it works. Thanks so so much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles