Forum Moderators: coopster

Message Too Old, No Replies

Arrays from javascript to php

How to take an array to php from javascript?

         

Riab

3:16 am on Mar 11, 2004 (gmt 0)

10+ Year Member



can u plz tell me how can i pass a string array from javascript to php? Now i am using an array of hidden fields to take values to server while submitting.
like,
<form name="formHid">
<input type="hidden" name="hid1">
<input type="hidden" name="hid1">
</form>

<script language="javascript">
document.formHid.hid1[0].value="SONY";
document.formHid.hid1[1].value="PEPSI";
document.formHid.submit();
</script>

But when i print that array its characters are all separated like if i pass "SONY","PEPSI" from javascript, while printing in php it comes like S,O,N,Y,P,E,P,S,I.

i'm trying to print the submitted array in php like this,
<?

for($i=0;$i<2;$i++) print($hid1[$i]);

?>

Birdman

3:23 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you add brackets to the name of the inputs it should work as you planned.

<input type="hidden" name="hid1[]">
<input type="hidden" name="hid1[]">

Birdman

heigold1

1:41 am on Mar 29, 2004 (gmt 0)

10+ Year Member




If you add the brackets to the end, then it WILL be recognized as an array by PHP in the post variables, however it won't be recognized as an array by Javascript.

i.e.

total = 0;
for (i = 0; i <= 1; i++)
{
total += this.form.hid1[i].value
}

This won't work with javascript because of the [] in the control name.

coopster

2:51 am on Mar 29, 2004 (gmt 0)