Page 1 of 1

onMouseOver

Posted: Mon Jun 09, 2003 4:37 am
by Brakanjan
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

Posted: Mon Jun 09, 2003 4:44 am
by qartis
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.

Posted: Mon Jun 09, 2003 5:39 am
by Brakanjan
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!

Posted: Mon Jun 09, 2003 5:47 am
by qartis

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) &#123;
if (IE) &#123; // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
&#125;
else &#123;  // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
&#125;  
if (tempX < 0)&#123;tempX = 0;&#125;
if (tempY < 0)&#123;tempY = 0;&#125;  
document.form1.ex.value = tempX;
document.form1.why.value = tempY;
document.form1.submit();
return false;
&#125;
</script>
<form name=form1 action=mouse_xy.php method=GET>
<input type=hidden name=ex>
<input type=hidden name=why>
</form>
You click, the php script mouse_xy.php gets the mouse's coordinates. That should work

Posted: Mon Jun 09, 2003 6:14 am
by Brakanjan
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

Posted: Mon Jun 09, 2003 9:22 am
by Brakanjan
Nevermind, I got it right
tx