php variable inside j.script function return [object Event]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
azhan
Forum Commoner
Posts: 68
Joined: Fri Jun 27, 2008 6:05 am

php variable inside j.script function return [object Event]

Post by azhan »

hello guys,

on my first load page, I call function init on php page like below

<script type="text/javascript">function initView('<? echo $username; ?>');</script>

whereby the php variable $username already declare on top...

but when im try to alert() the variable back in javascript..it return [object Event] which by mean javascript could not understand the php variable i guess.

Im thinking my parse on the variable could be wrong which i do not know how to play with ' and " to wrap the php variable properly on javascript function.

Could somebody do some correction on the code above?

Thanks!!
Azhan
jaiswarvipin
Forum Commoner
Posts: 32
Joined: Sat Sep 12, 2009 3:43 pm
Location: India

Re: php variable inside j.script function return [object Event]

Post by jaiswarvipin »

Hi,
you are right. do same was but follow the below steps like

1) Pelase change you code to as below

Code: Select all

<script type="text/javascript">function initView('<?php echo $username; ?>');</script>
2) After lodaing hte page in testing / developement browser then check the view sorce. How it look means is funciton contains some string or some thing else
2.1) If some thing else then pelase check the top.php where you $username variable is not contains proper value.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php variable inside j.script function return [object Event]

Post by AbraCadaver »

You're ecohing the contents of a PHP variable in what appears to be a mix of a function definition and a function call which makes no sense. Here is an example of what I think you want:

Code: Select all

<script type="text/javascript">
 
function initView(username) {
    alert(username);
}
initView('<?php echo $username; ?>');
 
</script>
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.
Post Reply