Connecting Tech Pros Worldwide Help | Site Map

php array in javascript function

Obscurr
Guest
 
Posts: n/a
#1: Jul 17 '05
i've got an array created in php that I want to send over to a javascript function.

onclick = "return sjekk(<?echo $names; ?> )"

in my "sjekk" function, when I try to print an alert :

function sjekk(navn)
{
alert(navn); // or alert(navn[0])
return(false);
}

I get "function Array { [native code]}"

How do I print out the names I want?

obscurr
jn
Guest
 
Posts: n/a
#2: Jul 17 '05

re: php array in javascript function


"Obscurr" <obscurr@hotmail.com> wrote in message
news:4452c409.0310200045.46f72e37@posting.google.c om...[color=blue]
> i've got an array created in php that I want to send over to a javascript[/color]
function.[color=blue]
>
> onclick = "return sjekk(<?echo $names; ?> )"
>
> in my "sjekk" function, when I try to print an alert :
>
> function sjekk(navn)
> {
> alert(navn); // or alert(navn[0])
> return(false);
> }
>
> I get "function Array { [native code]}"
>
> How do I print out the names I want?
>
> obscurr
>[/color]

Give your function a string created from your array instead:
<?
$name_string = implode( ',', $names);
?>
onclick = "return sjekk(<?echo $name_string; ?> )"


Erwin Moller
Guest
 
Posts: n/a
#3: Jul 17 '05

re: php array in javascript function


Obscurr wrote:
[color=blue]
> i've got an array created in php that I want to send over to a javascript
> function.
>
> onclick = "return sjekk(<?echo $names; ?> )"[/color]

try:

onclick = "return sjekk('<? echo $names; ?>')"

that is with a space between the <? and echo
and
with '' around your string.
In general: Look into your HTML produced by your PHP to find out why
Javascript has trouble with it.
Keep in mind your browser hasn't got a clue what process produced the HTML
in the first place.
Perl/PHP/C/ASP/Servlets etc.etc. it doesn't matter, only the output send to
the browser matters.
So always check that first.

Regards,
Erwin Moller
[color=blue]
>
> in my "sjekk" function, when I try to print an alert :
>
> function sjekk(navn)
> {
> alert(navn); // or alert(navn[0])
> return(false);
> }
>
> I get "function Array { [native code]}"
>
> How do I print out the names I want?
>
> obscurr[/color]

Closed Thread