How do you pass an array from PHP to html-code?

My way to do this is to pass the array as a string to a formfield and then use javascript to put the values into an array. Is this a good method?

I've seen some coders suggest to use the htmlspecialchars-function combined with serialization. Does anyone have an example for how to do this?

What is the better way to do it? What is the most secure?

Thanks.

    This does not make sense.

    HTML does not recognize arrays, as far as I know, as it is not a programming language?

    What are you tryiing to do?

    J.

      I want to pass an array to the html-code. The array will contain some information and I will use javascript to manipulate this info. I could have done the same programming with php instead of javascript, but then I need to load the page over and over again.

      Below is a simplyfication of what I want to do:

      <?
      $myArr = array(1, 2, 3, 4);
      ?>

      <HTML>
      <SCRIPT>
      Here I want to have some javescriptcode that shows info stored in the array "arr".
      </SCRIPT>

      <form>
      <input type="hidden" name="arr[]" value='<? echo $myArr ?>' />
      </form>

      </HTML>

      What is the advantage of useing serialization the htmlspecialchars-function?

        Why not pass it into the Javascript directly instead? Have PHP write a Javascript array, in other words.

          Now I passed an array to an array in javascript. When I do it the way I do in ex1 I get an error. When I pass it using the two functions htmlentities() and serialize() the javascriptarray and the formarray get some values. It seems though that the arrays aren't unserialized. Does anyone know how to do that?

          PHP
          <?
          $myArr = array(1, 2, 3, 4);
          ?>

          <HTML>
          <SCRIPT>

          ex1:
          arr1 = new Array();
          arr1 = '<? echo $myArr; ?>';

          ex2:
          arr1 = new Array();
          arr1 = '<? echo htmlentities( serialize( $myArr ) ); ?>';

          </SCRIPT>

          ex1:
          <form>
          <input type="hidden" name="arr[]" value='<? echo $myArr; ?>' />
          </form>

          ex2:
          <form>
          <input type="hidden" name="arr[]" value='<? echo htmlentities( serialize( $myArr ) ); ?>' />
          </form>

          </HTML>

            php and javascript array have nothing in common so dont think that a serialized php array will make any sense whatsoever to javascript. It simply wont.

            Try...

            arr1 = '<?php echo implode(',',$myArr); ?>';
            
              Write a Reply...