Page 1 of 1

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

Posted: Sun Jan 31, 2010 4:47 am
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

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

Posted: Thu Feb 04, 2010 5:55 am
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.

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

Posted: Thu Feb 04, 2010 10:40 am
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>