5 Replies - 577 Views - Last Post: 01 October 2012 - 05:50 AM Rate Topic: -----

#1 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

calling a function in php

Posted 30 September 2012 - 10:07 AM

Ok so i have a map script were if the user presses up down left or right on the keyboard then the image moves. All is working fine but now i want to add buttons so that when the user presses the button it will do the same thing. So i would have a up arrow image and then when the user clicks it it would call the same function has the upkey on the keyboard.

so here is my functions in a js file



function move(d)
{
	if(d == "UP")
	{
		var top = (parseInt(document.getElementById('move').style.top) - 10);
		var left = parseInt(document.getElementById('move').style.left);
		if(canMove(new xy(left,top)))
		{
			document.getElementById('move').style.top = (parseInt(document.getElementById('move').style.top) - 10) + 'px';
			document.player.src = "maps/sprites/playerUp.png";
			//if(lastStep != "UP") clearTimeouts();
			//setTimeout('document.player.src="playerUp.png"', 500);
			//lastStep = "UP";
			reloadF();
		}
	}
	else if(d == "DOWN")
	{
		var top = (parseInt(document.getElementById('move').style.top) + 10);
		var left = parseInt(document.getElementById('move').style.left);
		if(canMove(new xy(left,top)))
		{
			document.getElementById('move').style.top = (parseInt(document.getElementById('move').style.top) + 10) + 'px';
			document.player.src = "maps/sprites/playerDown.png";
			//if(lastStep != "DOWN") clearTimeouts();
			//setTimeout('document.player.src="playerDown.png"', 500);
			//lastStep = "DOWN";
			reloadF();
		}
	}
	else if(d == "RIGHT")
	{
		var top = parseInt(document.getElementById('move').style.top);
		var left = (parseInt(document.getElementById('move').style.left)+10);
		if(canMove(new xy(left,top)))
		{
			document.getElementById('move').style.left = (parseInt(document.getElementById('move').style.left) + 10) + 'px';
			document.player.src = "maps/sprites/playerRight.png";
			//if(lastStep != "RIGHT") clearTimeouts();
			//setTimeout('document.player.src="playerRight.png"', 500);
			//lastStep = "RIGHT";
			reloadF();
		}
	}
	else if(d == "LEFT")
	{
		var top = parseInt(document.getElementById('move').style.top);
		var left = (parseInt(document.getElementById('move').style.left)-10);
		if(canMove(new xy(left,top)))
		{
			document.getElementById('move').style.left = (parseInt(document.getElementById('move').style.left) - 10) + 'px';
			document.player.src = "maps/sprites/playerLeft.png";
			//if(lastStep != "LEFT") clearTimeouts();
			//setTimeout('document.player.src="playerLeft.png"', 500);
			//lastStep = "LEFT";
			reloadF();
		}
	}
	handled = true;
}






So now i have a up arrow image and would like to call the UP function so i try and do it like so.

<img src="myflile.png" width="61" height="57" onclick="move('UP')" />



but seems to be un clickable.... even tho i include the function page to the page.

Is This A Good Question/Topic? 0
  • +

Replies To: calling a function in php

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: calling a function in php

Posted 30 September 2012 - 11:10 AM

PHP cannot call Javascript functions because they're not processed on the same computer!

You could set up the HTML (using PHP) so that a Javascript function is called:
echo "call_some_js();"; // If this is in the right place in the HTML, then this function will be called after loading. 


There is also Javascript's onload.

This post has been edited by CTphpnwb: 30 September 2012 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: calling a function in php

Posted 30 September 2012 - 11:16 AM

View PostCTphpnwb, on 30 September 2012 - 06:10 PM, said:

PHP cannot call Javascript functions because they're not processed on the same computer!

You could set up the HTML (using PHP) so that a Javascript function is called:
echo "call_some_js();"; // If this is in the right place in the HTML, then this function will be called after loading. 


There is also Javascript's onload.


Well i need it so when i click a image it will load a java script function. So i would have 4 images up down left and right and then when the user clicks up it will call the d == "UP" function etc...


could i do

<img src="myflile.png" width="61" height="57" onclick="<?php   echo "UP();";   ?>" />


The function i want to call in a java script function and not a php function by the way.

This post has been edited by rpgmaker: 30 September 2012 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: calling a function in php

Posted 01 October 2012 - 04:52 AM

You could echo the function call (after fixing your quotes), but why? If that function isn't going to change then why not do this?
<img src="myflile.png" width="61" height="57" onclick="UP();" />

Was This Post Helpful? 0
  • +
  • -

#5 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: calling a function in php

Posted 01 October 2012 - 05:37 AM

View PostCTphpnwb, on 01 October 2012 - 11:52 AM, said:

You could echo the function call (after fixing your quotes), but why? If that function isn't going to change then why not do this?
<img src="myflile.png" width="61" height="57" onclick="UP();" />

That does not work... The image is unclickable ...
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: calling a function in php

Posted 01 October 2012 - 05:50 AM

Then your Javascript function is not defined properly. I suggest you post that problem in the Javascript forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1