?
Solved

Using PHP to get Javascript Array

Posted on 2007-10-15
5
Medium Priority
?
1,504 Views
Last Modified: 2008-01-09
Hi i have javascript Array for and i would like to pass it to the next page which is coded in PHP

Problem is I tried methods found in this site but all seem out of luck

In HTML JAVASCRIPT PAGE

blendArray = (1,2,3,4,5,6,7,8);

function setValue(){
  document.test.arv.value = blendArray;
  alert (document.test.arv.value);
}

<form action="phpArrayTest.php" method=post name=test onSubmit=setValue()>
<input name=arv type=hidden>
<input type=submit>

In PHP i use

$getArray = $_POST['arv'];

and print out $getArray but nothing shown.
0
Comment
Question by:chockmilk
5 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 20083351
you could use:

document.forms['test'] .arv.value = blendArray.join(";");

And in php use:

$getArray = explode(';', $_POST['arv']);

-r-
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 80 total points
ID: 20083460
Correct syntax is
blendArray = [1,2,3,4,5,6,7,8];

Does it alert?

Here is the canonical way to do this:

<script>
function setValue(theForm){
  theForm.arv.value = blendArray.toString(); // or blendArray.join(",") as Roonan suggested
  alert (theForm.arv.value);
}
</script>

<form action="phpArrayTest.php" method="post" onSubmit="setValue(this)">
<input name=arv type=hidden>
<input type=submit>
</script>

0
 
LVL 3

Accepted Solution

by:
KhoiNqq earned 120 total points
ID: 20083482
To access the post data as array, you should post the array item as separate hidden input with the name in form: arv[] (like the PHP array format)

<script type="text/javascript">
  blendArray = [1,2,3,4,5,6,7,8]; // using [ ] for array define instead of ( )
  function onSubmit(){
   var oF = document.getElementById("test");
   setArray("arv", blendArray);
  }
  function setArray(name, arr){
   var oF = document.getElementById("test");
   for (var i = 0; i < arr.length; i++){
      var iH = document.createElement('input');
      oF.appendChild(iH);
        iH.type="hidden";
        iH.name=name+"["+i+"]";
        iH.value=arr[i];
   }
  }
</script>
<form action="phpArrayTest.php" method="post" name="test" id="test" onSubmit="onSubmit()">
<input type="submit" />
</form>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20083488
Yikes :)
0
 

Author Comment

by:chockmilk
ID: 20084078
thanks alot guy for helping me out

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question