js array -> php array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?



Re: js array -> php array

Quoted text here. Click to load it

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 !
'   `)   /
 |\_ =="

Re: js array -> php array

denisb wrote:
Quoted text here. Click to load it

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.php" method="post">
    <input type="text" name="testText">
    <input type="hidden" name="testHidden">
    <input type="submit">

<script language="javascript">
    testArray = new Array (1,2,3);
    document.testForm.testHidden.value = testArray;


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


Site Timeline