Embedding PHP variables in Javascript

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
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Embedding PHP variables in Javascript

Post by NotOnUrNelly »

Weirdan | Please use

Code: Select all

,

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

,

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]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Shouldn't it be

Code: Select all

onMouseOver="P7AniMagic('<?php echo $Layer2Scroll; ?>',0,<?php echo $ScrollVal; ?>,5,30,0,0)"
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post by NotOnUrNelly »

Weirdan | Please use

Code: Select all

,

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

,

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]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post 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
Last edited by NotOnUrNelly on Sun Sep 07, 2008 5:31 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post 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.

Code: Select all

 
 $Layer2Scroll = initial;
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.
Last edited by NotOnUrNelly on Sun Sep 07, 2008 5:32 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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) ?
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post by NotOnUrNelly »

Yes all my functions mix PHP and Html, do you think thats whats causing it.
NotOnUrNelly
Forum Commoner
Posts: 61
Joined: Wed Mar 24, 2004 4:45 pm

Post 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

Code: Select all

$Layer2Scroll = initial;
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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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 }
Post Reply