Forum Moderators: coopster

Message Too Old, No Replies

Call JS Functions dynamically with PHP

Call JS Functions dynamically with PHP

         

Lee_De_Vil

6:58 pm on Jul 6, 2003 (gmt 0)



Hi,

I want to change which Javascript Function to call by clicking on an image with PHP.

I have 12 identical images on the one page which use the same image map

each image has a link like so:

<a><img usemap="#imagemap" src="image.gif"></a>

and the image map:

<map name="imagemap">
<area shape="rect" coords="1,1,7,10"
href="javascript:funk1('#00FF00')">

etc...

the Javascript:

function funk1(val) {
document.form.text_input.value = val;
}
function funk2(val) {
document.form.text_input2.value = val;
}

I'm looking at changing:

"javascript: [b][2]funk1[/2][/b] ('#00FF00')"

to

"javascript: [b][2]funk2[/2][/b] ('#00FF00')"

by using PHP like so:

href="javascript:<?php echo($funk_number);?>('#00FF00')">

how do I send a value across from the image? tried echoing a value from between the link tags, this worked in a way but not quite how i'd like.

wruk999

7:19 pm on Jul 6, 2003 (gmt 0)

10+ Year Member



Hi Lee_de_Vil,

Firstly, welcome to WebmasterWorld [webmasterworld.com]

Could you do something, similar to how you have mentioned, so that the variable is passed as a parameter in the url.

ie: [example.com?funk=1...]

then at the top of your page:


<?
$funk = ($HTTP_GET_VARS['funk']);
//get the funk id and set it into the 'funk' value.


if(!$funk) {
$funk= "funk1";
}
//If there is no value in the parameter...set it to 1. (A ssort of basic error-checking)
?>

Then in your javascript, as you have pointed out, just:


href="javascript:<?php echo $funk? >('#00FF00')">

Is this what you are looking for?
wruk999

<edit>Editied for clarity</edit>

dougmcc1

3:37 am on Jul 7, 2003 (gmt 0)

10+ Year Member



Lee_De_Vil,

If I am following you right, I think you could do something like this:

<a><img usemap="#imagemap" src="image.gif" onMouseClick=(var=1)></a>
<a><img usemap="#imagemap" src="image.gif" onMouseClick=(var=2)></a>
<a><img usemap="#imagemap" src="image.gif" onMouseClick=(var=3)></a>

then for your javascript:
if ( var==1)
document.form.text_input.value = val;
else if ( var==2)
document.form.text_input2.value = val;
else
document.form.text_input3.value = val;

PHP is parsed before your page is served to your visitor so clicking on an image wouldn't have any effect if PHP is used to change the value when they click on an image.

wruk999

6:31 am on Jul 7, 2003 (gmt 0)

10+ Year Member



I didn't quite understand that you need it doing on the fly.

Whoops! In that case, as dougmcc1 as said, PHP is parsed on the server then sent to the client, so PHP can't be used for that.

wruk999.