Problem in passing php variable in a javascipt function

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
K-Z
Forum Commoner
Posts: 29
Joined: Mon Jul 05, 2010 9:03 am

Problem in passing php variable in a javascipt function

Post 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??
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Problem in passing php variable in a javascipt function

Post 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.
K-Z
Forum Commoner
Posts: 29
Joined: Mon Jul 05, 2010 9:03 am

Re: Problem in passing php variable in a javascipt function

Post 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...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Problem in passing php variable in a javascipt function

Post by AbraCadaver »

You need to look up how to do things in JS: This should work:
[text]document.location = 'usergreenroom.php?visit=' + v;[/text]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
K-Z
Forum Commoner
Posts: 29
Joined: Mon Jul 05, 2010 9:03 am

Re: Problem in passing php variable in a javascipt function

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