php array in javascript function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Obscurr

    #1

    php array in javascript function

    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

    #2
    Re: php array in javascript function

    "Obscurr" <obscurr@hotmai l.com> wrote in message
    news:4452c409.0 310200045.46f72 e37@posting.goo gle.com...[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; ?> )"


    Comment

    • Erwin Moller

      #3
      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]

      Comment

      Working...