js array -> php array

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

    js array -> php array

    Hi,

    I have an array in javascript of which the contents are manipulated by
    the user. When he is done, he calls a function to reload the page. Then
    conclusion depending on the input should be printed.
    The problem is, how can I use the javascript array in this php file?
    Is it neccesary to use numerous hidden form fields for this purpose?

    regards,

    Roderik
  • denisb

    #2
    Re: js array -> php array

    Roderik <mail@roderik.n et> wrote:[color=blue]
    > The problem is, how can I use the javascript array in this php file?
    > Is it neccesary to use numerous hidden form fields for this purpose?[/color]

    cause javascript is executed on client browser and php script on server
    : yes.

    you have 3 methods for passing javascript values to your php script :

    1- hidden (or not) fields of a form ;
    2- link with url like www.script.php?var1=val1&var2=val2...
    /!\ : you must serialize ?var1=val1&var2 =val2... :
    3- cookie string

    --
    @@@@@
    E -00 comme on est very beaux dis !
    ' `) /
    |\_ =="

    Comment

    • Sadara

      #3
      Re: js array -&gt; php array

      denisb wrote:[color=blue]
      > Roderik <mail@roderik.n et> wrote:
      >[color=green]
      >>The problem is, how can I use the javascript array in this php file?
      >>Is it neccesary to use numerous hidden form fields for this purpose?[/color]
      >
      >
      > cause javascript is executed on client browser and php script on server
      > : yes.
      >
      > you have 3 methods for passing javascript values to your php script :
      >
      > 1- hidden (or not) fields of a form ;
      > 2- link with url like www.script.php?var1=val1&var2=val2...
      > /!\ : you must serialize ?var1=val1&var2 =val2... :
      > 3- cookie string
      >[/color]

      further to roderick's reply:

      an array passed back to the server in a hidden form field will arrive in
      your PHP script as a comma-delimited list, either in a POST variable, or
      a GET variable.

      this example uses the POST method --

      <form name="testForm" action="test.ph p" method="post">
      <input type="text" name="testText" >
      <input type="hidden" name="testHidde n">
      <input type="submit">
      </form>

      <script language="javas cript">
      testArray = new Array (1,2,3);
      document.testFo rm.testHidden.v alue = testArray;
      </script>

      <?php

      if (isset($_POST['testHidden'])) {
      $tok = strtok($_POST['testHidden'], ",");
      while ($tok) {
      echo "element = $tok<br>";
      $tok = strtok (",");
      }
      }

      ?>

      Comment

      Working...