Page 1 of 1

Problem in passing php variable in a javascipt function

Posted: Tue Jul 27, 2010 10:50 am
by K-Z
Hi all...is it possible to pass a php variable to a javascript function within a page. That is, if you define a javascript function in a php page, is it possible to call that function from the same page only.

Code: Select all

Example: if the function is Func() defined in javascript....I want to pass $x in it:

<script type="text/javascript">
function Func(x) {......}
</script>

<?php if($x==0) { ?>
<body onload="Func($x)"></body>
But its not happening. Whts wrong??

Re: Problem in passing php variable in a javascipt function

Posted: Tue Jul 27, 2010 10:54 am
by buckit

Code: Select all

<?php if($x==0) { ?>
<body onload="Func(<?php echo $x;?>)"></body>
$x is a php variable so you need to call it in PHP.

Re: Problem in passing php variable in a javascipt function

Posted: Tue Jul 27, 2010 11:48 am
by K-Z
buckit wrote:

Code: Select all

<?php if($x==0) { ?>
<body onload="Func(<?php echo $x;?>)"></body>
$x is a php variable so you need to call it in PHP.
Thanks for the reply but its not working in my case...The function is message1() and variable being passed is $x. The message() function after showing an alert moves to a new url (<a href=\"usergreenroom.php?visit="v"\">) for which $x is required. Below is the code:

Code: Select all

<head>
<script language="javascript">
function message1(v)
{

alert("Being of lower level, you are not allowed to view his posts");
document.location.href='<a href=\"usergreenroom.php?visit="v"\">';
return true;
}
</script>
</head>

<?php  if($x>$y) { ?>
	<body onload="message1(<?php echo $x; ?>)"></body> <?php } 
   else { 
    echo $x;
    ?>
Please help me...

Re: Problem in passing php variable in a javascipt function

Posted: Tue Jul 27, 2010 11:57 am
by AbraCadaver
You need to look up how to do things in JS: This should work:
[text]document.location = 'usergreenroom.php?visit=' + v;[/text]

Re: Problem in passing php variable in a javascipt function

Posted: Tue Jul 27, 2010 12:02 pm
by K-Z
AbraCadaver wrote:You need to look up how to do things in JS: This should work:
[text]document.location = 'usergreenroom.php?visit=' + v;[/text]
Thank you very much....I will definitely read javascript once again..... :wink: :wink: :wink: