Hi, I want to write a php script that display the current coordinates (pixels) of the mouse.
Usually in javascripts you would use something like
window.captureEvents(Event.MOUSEMOVE);
window.onMouseMove = OutputCoords;
How can I achieve this in PHP?
tx
onMouseOver
Moderator: General Moderators
You can't. PHP is executed on the server, and the output is sent to the client's browser. The output is the only code that can get the mouse's coordinates, or other client-side stuff, which is what javascript is for. Consider writing some javascript that will capture the mouse's coordinates and redirect to a .php script to deal with the coordinates. Something like mouse_xy.php?x=456&y=123. If you can provide more details on your application, I could write up a snippet for you.
Hi
tx for the reply.
I want to get variables of the current mouse position, so that I can show the current coords of the pixles (x;y) on the page. (onMouseOver,javascript?) I want to be able to use these coords as php variables, that's why I don't know how to combine javascript and php. I would probably only use these variables in php for a onMouseClick, which I suppose would make the coding easier...
The other problem I have is that my pixels don't begin at 0;0, it depends on a php variable I determine beforehand!
tx for the reply.
I want to get variables of the current mouse position, so that I can show the current coords of the pixles (x;y) on the page. (onMouseOver,javascript?) I want to be able to use these coords as php variables, that's why I don't know how to combine javascript and php. I would probably only use these variables in php for a onMouseClick, which I suppose would make the coding easier...
The other problem I have is that my pixels don't begin at 0;0, it depends on a php variable I determine beforehand!
Code: Select all
<script language="Javascript">//Taken from codelifter.com
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onclick = getMouseXY;
var tempX = 0;
var tempY = 0;
function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else { // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}
document.form1.ex.value = tempX;
document.form1.why.value = tempY;
document.form1.submit();
return false;
}
</script>
<form name=form1 action=mouse_xy.php method=GET>
<input type=hidden name=ex>
<input type=hidden name=why>
</form>tx, it works excellent.
Can you please check the mistake I'm making while trying to include a onMouseOver function:
document.onclick = getMouseXY;
document.onMouseOver = ShowMouse;
var tempX = 0;
var tempY = 0;
function ShowMouse(){
var s = '<DIV>X = '+tempx+'.<BR> Y = '+tempy'.</DIV>';
document.writeln(s);
}
Also, I have a php file with initial pixel values. How do I post them to tempX, tempY?
tx
Can you please check the mistake I'm making while trying to include a onMouseOver function:
document.onclick = getMouseXY;
document.onMouseOver = ShowMouse;
var tempX = 0;
var tempY = 0;
function ShowMouse(){
var s = '<DIV>X = '+tempx+'.<BR> Y = '+tempy'.</DIV>';
document.writeln(s);
}
Also, I have a php file with initial pixel values. How do I post them to tempX, tempY?
tx