Connecting Tech Pros Worldwide Forums | Help | Site Map

js array -> php array

Roderik
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: js array -> php array


Roderik <mail@roderik.net> 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 !
' `) /
|\_ =="
Sadara
Guest
 
Posts: n/a
#3: Jul 17 '05

re: js array -> php array


denisb wrote:[color=blue]
> Roderik <mail@roderik.net> 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.php" method="post">
<input type="text" name="testText">
<input type="hidden" name="testHidden">
<input type="submit">
</form>

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

<?php

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

?>
Closed Thread