View Full Version : pass varible from php to javascript
kamkam
09-27-2007, 11:15 AM
Hi;
i am trying to pass a varible from php to javascript, but it does not work. i could print out the value of
Could you help me, please.
i could print out the value of $test from the following:
<?php $test=" me "; ?>
<script type="text/javascript">
function myTest(){
alert(" this is " + <?php $test; ?>);
}
</script>
<input type="button" id="b3" name="b3" value="3" onClick="myTest()" />
you need to print or echo out the variable
<?php $test=" me "; ?>
<script type="text/javascript">
function myTest(){
alert(" this is " + <?php echo $test; ?>);
}
</script>
cheers,
~E
_Aerospace_Eng_
09-27-2007, 02:03 PM
you need to print or echo out the variable
<?php $test=" me "; ?>
<script type="text/javascript">
function myTest(){
alert(" this is " + <?php echo $test; ?>);
}
</script>
cheers,
~E
That won't work because you are adding a string. Your result gives this
<script type="text/javascript">
function myTest(){
alert(" this is " + me );
}
</script>
which will error out, me would not be considered as a string, it would be considered as a variable and its not defined anywhere.
This will work
<?php $test=" me "; ?>
<script type="text/javascript">
function myTest(){
alert(" this is<?php echo $test; ?>");
}
</script>
The result
<script type="text/javascript">
function myTest(){
alert(" this is me ");
}
</script>
projectxmatt
09-27-2007, 07:19 PM
This will work
<?php $test=" me "; ?>
<script type="text/javascript">
function myTest(){
alert(" this is<?php echo $test; ?>");
}
</script>
The result
<script type="text/javascript">
function myTest(){
alert(" this is me ");
}
</script>
or if your more partial to the short hand like myself
<?php $test=" me "; ?>
<script type="text/javascript">
function myTest(){
alert(" this is<?=$test?>");
}
</script>
Fumigator
09-27-2007, 07:27 PM
Which is fine until you move your code to a server that has short tags disabled :eek:
kamkam
09-28-2007, 12:59 PM
Thanks for all, it works now.
But why i put the javascript code into the <head> </head> tags, it could not print out the values of $test.
my code as following:
<html>
<head>
<script type="text/javascript">
function myTest(){
alert(" this is<?php echo $test; ?>");
}
</script>
</head>
<body>
<?php $test=" me "; ?>
<input type="button" id="b3" name="b3" value="3" onClick="myTest()" />
</body>
</html>
CFMaBiSmAd
09-28-2007, 01:41 PM
The problem is not that the javascript is in the <head> </head> it is that the PHP variable $test is being echoed before it has had a value assigned to it.
Your PHP assignment statement <?php $test=" me "; ?> is later in the source code and is executed after the echo statement.
_Aerospace_Eng_
09-28-2007, 05:42 PM
Which is fine until you move your code to a server that has short tags disabled :eek:
Yep, which is why I didn't offer that as a solution.
projectxmatt
09-28-2007, 06:06 PM
yea true, i just haven't encountered one that is disabled yet (keyword yet).
kamkam
09-28-2007, 09:46 PM
Thanks for all.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.