0

So I adapted this code from another function that does work and is used on the same page. I don't know if that could do anything to it, so I thought I'd mention that, and also that both have id="replace" in them, which I don't know if it could interfere.

Anyway, the code to call the function is this:

 if (mysql_num_rows($LIKE) != 0)

{

  echo "
        <td id=\"replace\" style=\"text-align:left\">".$like." Likes -- <a style=\"color:#234904\" onmouseover=\"this.style.color='#6666CC'\" onmouseout=\"this.style.color='#234904'\" id=\"" . $POST->id . "\" onclick=\"likePost(this.id)\" name=\"like\">Like This Post</td>
        ";
}

the javascript function is defined as this:

var xmlHttp;

function likePost(x)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

var pid = document.getElementById(x).id;
var url="forum-like.php";
url+="?pid="+pid;
url+="&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

when I click on what should call the function, nothing happens. If I execute the script using the php page called in the js function, that works fine though... I don't know if I missed anything or anything is wrong though... I also know that the $POST->id part does call the right id... hope this is enough info, otherwise let me know and I'll edit in more :D Thanks for any help!

1
  • Open up Firefox + Firebug. Open the NET tab and look at your request/response. Commented May 25, 2012 at 19:44

2 Answers 2

1
var pid = document.getElementById(x).id

You're already passing the id so just use

var pid = id;

If you're still having issues then use firebug or your browser's developer tools to get the actual javascript error.

Sign up to request clarification or add additional context in comments.

3 Comments

I think it needs that though, because the javascript function is called in a separate document that is called from the header of the page where it's called? Not sure... also, I've just found that it does seem to register the function, but I need to refresh the page for it to update...
Elizabeth, if you can't change the js function then just change your php so that it doesn't pass (this.id), but instead just (this).
!! That worked! Except it seems to be interfering/mixing itself up with the other javascript function using id=replace... that possible?
0

It looks like you are passing this.id to the function, but then trying to use that (x) to then look up the id again as if that were a DOM object.

If you leave the php code as is, you can just change your javascript method to say:

url+="?pid="+x;

And you can get rid of: (since you are already passing the id and don't need to retrieve it)

var pid = document.getElementById(x).id;

Hope that works for you!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.