Jump to content

Unique span ids for ajax


harmor

Recommended Posts

I am trying to write an AJAX script that will display IMDb rating when I mouse over text. It works but it only displays the rating on the first result due to it being the first span tag it finds.

Here is the page: http://www.bobtownmctchargeumc.org/com/index.php

Hover over "Rating:"

 

Here is the JS

var url = "server_script.php?title="; // The server-side script
   
function handleHttpResponse() {   
    if (http.readyState == 4) {
          if(http.status==200) {
              var results=http.responseText;
         	 	document.getElementById('imdbrating').innerHTML = results;
         	 //alert(results);
          }
          }
    }
   
function getimdbrating(title) {     
    //var sId = document.getElementById("ajax_res").value;
    http.open("GET", url + title, true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}
function getHTTPObject() {
  var xmlhttp;

  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
   
}
  return xmlhttp;


}
var http = getHTTPObject(); // We create the HTTP Object

 

Here is the html.

<span onmouseover=\"getimdbrating('".$movRows['imdb']."');return false;\">Rating: </span> <span id='imdbrating'></span>

 

 

I want the rating to display next to the movie.

 

Link to comment
https://forums.phpfreaks.com/topic/144508-unique-span-ids-for-ajax/
Share on other sites

2 ways I can think of doing this off hand:

-Add a counter to the spans. [explanation provided]

-Get the DOM next sibling(?). [not well versed in this, but you could look it up]

 

When PHP builds that line:

<span onmouseover=\"getimdbrating('".$movRows['imdb']."');return false;\">Rating: </span> <span id='imdbrating'></span>

have a counter going.

 

For example:

//while looping through database results
$i=0;
while(...) {
  echo "<span onmouseover=\"getimdbrating('".$movRows['imdb']"','$i');return false;\">Rating: </span> <span id='imdbrating$i'></span>"
$i++
}

And modify your ajax function to accept the id of the span to change.

 

function getimdbrating(title,spanid){
  var spanToChange = document.getElementById("imdbrating"+spanid);
..
}

or something similar.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.