Page 1 of 1
Embedding PHP variables in Javascript
Posted: Thu Sep 21, 2006 5:01 am
by NotOnUrNelly
Weirdan | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi all,
Just a quick question, Im sure it is very simple, I have a line of HTML code that calls a javascript function. The function basically scrolls layers. In order to scroll the javascript is sent a number of variable including in my case
A layer
and a value to scroll down to
the code below is a sample of the code which works.
onMouseOver="P7AniMagic('initial',0,-200,5,30,0,0)"
the two key values are the word initial and the -200.
the initial is the name of the layer I wish to scroll and -200 is by how much I wish to scroll.
for each of these I need to send values to in the form of variables so any layer can be scrolled any distance.
I can replace the -200 with the variable $ScrollVal
onMouseOver="P7AniMagic('initial',0,<?php echo $ScrollVal; ?>,5,30,0,0)"
this works fine, i then want to replace the layer name in the following way.
Code: Select all
?>
onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,<?php echo $ScrollVal; ?>,5,30,0,0)"
This does not work, Im sure I have some of the "'". the wrong way around.
Can anyone tell me how this should be wrote for characters, I seem to have managed the numbers by the $ScrollVal variable working okay.
The full code for that line is below
Code: Select all
<div id="scroller" style="position:absolute; width:63px; height:23px; z-index:12; left: 774px; top: 275px; visibility: visible;"><img src="images/del.jpg" width="31" height="31" onMouseOver="P7AniMagic('initial',0,0,5,30,0,0)" onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)"><img src="images/del.jpg" width="31" height="31" onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,<?php echo $ScrollVal; ?>,5,30,0,0)" onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)">
</div>
Many Thanks
jamie
Weirdan | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Sep 21, 2006 5:05 am
by CoderGoblin
Shouldn't it be
Code: Select all
onMouseOver="P7AniMagic('<?php echo $Layer2Scroll; ?>',0,<?php echo $ScrollVal; ?>,5,30,0,0)"
Posted: Thu Sep 21, 2006 5:16 am
by NotOnUrNelly
Weirdan | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi Thanks for your reply,
that did not work, what does work is if I change the code to
onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,0,5,30,0,0)"
and then decalre the variable $Layer2Scroll as a value before hand.
$Layer2Scroll = initial;
this is very strange.
My full code is as follows
Code: Select all
<?php
function scroller_set($upval,$Layer2Scroll)
{
$varTyi = gettype($Layer2Scroll);
echo $varTyi;
echo "Layer ".$Layer2Scroll;
$ScrollVal = $upval * -2000;
echo $scrollval;
$Layer2Scroll = initial;
//$Layer2Scroll = stripslashes($Layer2Scroll);
?>
<div id="scroller" style="position:absolute; width:63px; height:23px; z-index:12; left: 600; top: -30; visibility: visible;"><img src="images/del.jpg" width="20" height="20" onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,0,5,30,0,0)" onMouseOut="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,0,1,1,2,0)"><img src="images/del.jpg" width="20" height="20" onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,<?php echo $ScrollVal; ?>,5,30,0,0)" onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)">
</div>
<?php
}
?>
As you can see the function gets the variable $Layer2Scroll
I then check this value by echoing it to the screen.
This reads initial when echod to the screen.
As a test I then
redecalre the varaible "intial"
I then embed the <?php echo $Layer2Scroll ?> as previous.
This works fine, if I then remove the redeclaration, and just use the value sent to the function the code fails and my layer does not scroll.
Im finding this very strange.
thanks Again
Weirdan | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Sep 21, 2006 5:41 am
by CoderGoblin
For info.. Please use the php tags...
Code: Select all
<?php
function scroller_set($upval,$Layer2Scroll)
{
$retval='';
$varTyi = gettype($Layer2Scroll);
echo $varTyi; // assume for debug;
echo "Layer ".$Layer2Scroll; // assume for debug only;
$ScrollVal = $upval * -2000;
echo $scrollval;
$Layer2Scroll = initial;
//$Layer2Scroll = stripslashes($Layer2Scroll);
$retval.=<<<EOB
<div id="scroller" style="position:absolute; width:63px; height:23px; z-index:12; left: 600; top: -30; visibility: visible;">
<img src="images/del.jpg" width="20" height="20"
onMouseOver="P7AniMagic('{$Layer2Scroll}',0,0,5,30,0,0);"
onMouseOut="P7AniMagic('{$Layer2Scroll}',0,0,1,1,2,0);" />
<img src="images/del.jpg" width="20" height="20"
onMouseOver="P7AniMagic('{$Layer2Scroll}',0,{$ScrollVal},5,30,0,0);"
onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)" />
</div>
EOB;
return $retval;
}
echo(scroller_set($upval,$Layer2Scroll));
?>
You will notice I try not to mix PHP and HTML which may have been the cause of your problem. Try to follow the routine of process then output rather than mix the two. It makes things a lot easier to maintain.
Posted: Thu Sep 21, 2006 6:16 am
by NotOnUrNelly
Thansk for your reply,
I pasted your code and got the following error.
I think it is something to do with the
<?php
$retval.=<<<EOB
?>
I am quite new to this.
Many Thanks
Jamie
Posted: Thu Sep 21, 2006 6:51 am
by CoderGoblin
try:
Code: Select all
$retval.=<<<MYHEREDOC
<div id="scroller" style="position:absolute; width:63px; height:23px; z-index:12; left: 600; top: -30; visibility: visible;">
<img src="images/del.jpg" width="20" height="20"
onMouseOver="P7AniMagic('{$Layer2Scroll}',0,0,5,30,0,0);"
onMouseOut="P7AniMagic('{$Layer2Scroll}',0,0,1,1,2,0);" />
<img src="images/del.jpg" width="20" height="20"
onMouseOver="P7AniMagic('{$Layer2Scroll}',0,{$ScrollVal},5,30,0,0);"
onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)" />
</div>
MYHEREDOC;
using
Heredoc format as I hate backslashing variables when using a block like that.
Posted: Thu Sep 21, 2006 6:58 am
by NotOnUrNelly
Im Sorry,
Im still getting the same error message
Parse error: parse error, unexpected T_SL in /home/2478/sally-
even with the new code.
In the code that I posted previously, everything works if the variable $Layer2Scroll is declared as part of the function.
If I remove this and use the value sent to the function
Code: Select all
function scroller_set($upval,$Layer2Scroll)
{
$retval='';
$varTyi = gettype($Layer2Scroll);
echo $varTyi; // assume for debug;
echo "Layer ".$Layer2Scroll; // assume for debug only;
even though the variable sent to the function is set to the exactly the same value
its strange.
Posted: Thu Sep 21, 2006 7:05 am
by CoderGoblin
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
function scroller_set($upval,$Layer2Scroll)
{
$retval='';
$varTyi = gettype($Layer2Scroll);
echo $varTyi; // assume for debug;
echo "Layer ".$Layer2Scroll; // assume for debug only;
$ScrollVal = $upval * -2000;
echo $scrollval;
$Layer2Scroll = initial;
//$Layer2Scroll = stripslashes($Layer2Scroll);
$retval.=<<<MYHEREDOC
<div id="scroller" style="position:absolute; width:63px; height:23px; z-index:12; left: 600; top: -30; visibility: visible;">
<img src="images/del.jpg" width="20" height="20"
onMouseOver="P7AniMagic('{$Layer2Scroll}',0,0,5,30,0,0);"
onMouseOut="P7AniMagic('{$Layer2Scroll}',0,0,1,1,2,0);" />
<img src="images/del.jpg" width="20" height="20"
onMouseOver="P7AniMagic('{$Layer2Scroll}',0,{$ScrollVal},5,30,0,0);"
onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)" />
</div>
MYHEREDOC;
return $retval;
}
echo(scroller_set($upval,$Layer2Scroll));
?>
works for me. Do you have anything else in the file (mixing php and HTML) ?
Posted: Thu Sep 21, 2006 7:09 am
by NotOnUrNelly
Yes all my functions mix PHP and Html, do you think thats whats causing it.
Posted: Thu Sep 21, 2006 7:13 am
by NotOnUrNelly
If I was to use my original code, mixed
Code: Select all
function scroller_set($upval,$Layer2Scroll)
{
$retval='';
$varTyi = gettype($Layer2Scroll);
echo $varTyi; // assume for debug;
echo "Layer ".$Layer2Scroll; // assume for debug only;
$ScrollVal = $upval * -2000;
echo $scrollval;
$Layer2Scroll = initial;
//$Layer2Scroll = stripslashes($Layer2Scroll);
?>
<div id="scroller" style="position:absolute; width:63px; height:23px; z-index:12; left: 600; top: -30; visibility: visible;"><img src="images/del.jpg" width="20" height="20" onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,0,5,30,0,0)" onMouseOut="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,0,1,1,2,0)"><img src="images/del.jpg" width="20" height="20" onMouseOver="P7AniMagic(<?php echo "'".$Layer2Scroll."'" ?>,0,<?php echo $ScrollVal; ?>,5,30,0,0)" onMouseOut="P7AniMagic('initial',0,0,1,1,2,0)">
</div>
<?php
}
why do you think it is that when I remove
it stops working even though the value that is passed to the function is reading initial and its variable type is string.
Sorry I cant ge the correct method working.
Thanks for your help
Jamie
Posted: Thu Sep 21, 2006 7:18 am
by CoderGoblin
Most likely. My normal processing involves something like...
Code: Select all
<?php
function myfunct1()
{
// build retval
return $retval;
}
function myfunct2()
{
// build retval
return $retval;
}
function myfunct3()
{
// build retval
return $retval;
}
$body='';
$body.="Result1: ".myfunc1()."<br />Result1: ".myfunc2()."<br />Result1: ".myfunc3();
?>
<html>
<body>
what ever
<?php echo("$body"); ?>
whatever
</body>
</html>
I find this is far more maintable especially when it comes to matching { and }