onMouseOver

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Brakanjan
Forum Newbie
Posts: 19
Joined: Wed May 14, 2003 9:57 am
Location: South Africa

onMouseOver

Post 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
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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.
Brakanjan
Forum Newbie
Posts: 19
Joined: Wed May 14, 2003 9:57 am
Location: South Africa

Post 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!
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post 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
Brakanjan
Forum Newbie
Posts: 19
Joined: Wed May 14, 2003 9:57 am
Location: South Africa

Post 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
Brakanjan
Forum Newbie
Posts: 19
Joined: Wed May 14, 2003 9:57 am
Location: South Africa

Post by Brakanjan »

Nevermind, I got it right
tx
Post Reply