how to ask for jscript variable?

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
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

how to ask for jscript variable?

Post by lisawebs »

how to ask: if (jscript.variable then...)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

couldn't you just set the variable as a PHP variable?

ie, couldn't you just do this :

script language="JavaScript">
<!---
var currLoc = <?php echo $my_var; ?>

....

!-->

and just call it from within php?

like $my_var = 'something';

that way, you are controlling what that variable is in php, instead of trying to do it in jscript..

this is untested of course, but I'm almost sure it would work :P


edit : of course you would want to set that variable to a value BEFORE you call it in javascript...

so, you'd have


<?php

$my_var='something';

...

?>

<!---
var currLoc = <?php echo $my_var; ?>

....

!-->


all in one file...hope that helps
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

differences in syntax

Post by lisawebs »

your code didn't work, but

var variable = '<?php print "$phpvariable"; ?>'

worked fine, why???????
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

it's because i wasn't call it correctly in jscript. the echo/print doesn't matter, it's just how you call it in jscript ( which i'm not very familiar with ) that does.
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

why this doesn't work?

Post by lisawebs »

the following works fine:

echo("<script>");
echo("alert('hello')");
echo("</script>");

but inserting the following does not

echo(" var a = $variable ");

why??????
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

a very nice documentation on proper use of echo is found here : http://www.php.net/echo

if you are just trying to echo out $variable, just use this :

echo (" var a = ".$variable." ");
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

I read it before but (points) are not explained

Post by lisawebs »

I knew that, but nothing say about your (.points.)
what is that? why it works??
lisawebs
Forum Commoner
Posts: 44
Joined: Wed Nov 19, 2003 6:21 pm

it doesn't work either

Post by lisawebs »

no way,

there's something with the command var...
shows an error about "expected (,coma)"
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

because it exit's the echo statment, and adds the variable as a result.

take this for example :

if i do this :

$har = 'blah';
echo '$har';


then the word $har is going to show up on my screen, not blah.

however, if i do this :

$har = 'blah';
echo $har;

then the word blah appears on my screen...

so, you have to use that same reasoning when working with strings and variables inside the same echo statement.

so take this for example.

say we have a var named $word that is equal to 'world'.

so :

$word = 'world';

if we try to echo it like this :

echo 'hello $word!';

we are gonna get

hello $word! as a result.

isntead, we have to exit the string to add the variable to the string, like so :

$word = 'world';

echo 'Hello '.$word.'!';

which will produce :

Hello world!


again, view the php manual on more [php_man]echo[php_man] related commands.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

i used to just write
$foo = "the";
echo "$foo dog jumped over $foo brown log";

and it would print fine.. is there a setting that allows this or something?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

LiLpunkSkateR wrote:i used to just write
$foo = "the";
echo "$foo dog jumped over $foo brown log";

and it would print fine.. is there a setting that allows this or something?
Double quotes work differently to single quotes:
http://php.net/manual/en/language.types.string.php

Mac
Post Reply