Forum Moderators: open

Message Too Old, No Replies

Ajax problem with refresh

         

andrewsmd

10:29 pm on Dec 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a plus button that when clicks updates some data in a db with php and then changes the button to a different image. When this happens though, the page always goes back to the top. Is there some way to stop that? The script I use now works if you hit f5, but does not work when you click an image button. Here is how the js flows. I'm posting it in logical order

Here's the html image

<img id="plus17" src="images/buttons/plus.png" onclick="plusClick('17')" style="border: none;">

heres plusClick note: GetXmlHttpObject creates a valid ajax object I can post that if you need, I'm just trying to keep this short

function plusClick(id){

xmlHttp = GetXmlHttpObject();
var url = "updateMessage.php";
url = url + "?id=" + id + "&action=positive";
url = url + "&sid=" + Math.random();
xmlHttp.onreadystatechange = function () { messageUp(xmlHttp.responseText); };
xmlHttp.open("GET", url, true);
xmlHttp.send(null);

}//plusClick

Here is updateMessage.php
note: runQuery connects to the db and executes the query. It works fine.
$query = "update messages set up = up + 1 where id = {$id};";
if(runQuery($query)){

echo($id);

}//if runquery

As you can see I echo the id and then in the messageUp function called by the previous js function changes my image src to a green image meaning they clicked it.
function messageUp(value){

if(value.length > 0){
document.getElementById("plus"+value).src = "Images/buttons/plusGreen.png";
}//if

}//messageUp

Fotiman

11:32 pm on Dec 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't see anything in this example that would cause the page to scroll to the top. Is your <img> element wrapped in an <a>?

andrewsmd

11:43 pm on Dec 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh yes it is. I wanted to get the little finger when you hover over it. I'll try removing that on Monday and let you know if it works. It makes sense though, because I set that A tag to # because I wanted the hand. I'm guessing I can run a JS function to change the mouse pointer to a hand without putting it in an a tag right? I'll google it. Great catch, even without the code. I answer php posts on here as much as possible because whenever I have a question, you guys always come through. I love this site! I'll let you know how it goes.