Using PHP to get Javascript Array

Posted on 2007-10-15
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


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.
Question by:chockmilk
    LVL 49

    Expert Comment

    you could use:

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

    And in php use:

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

    LVL 75

    Assisted Solution

    by:Michel Plungjan
    Correct syntax is
    blendArray = [1,2,3,4,5,6,7,8];

    Does it alert?

    Here is the canonical way to do this:

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

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

    LVL 3

    Accepted Solution

    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');
    <form action="phpArrayTest.php" method="post" name="test" id="test" onSubmit="onSubmit()">
    <input type="submit" />
    LVL 75

    Expert Comment

    by:Michel Plungjan
    Yikes :)

    Author Comment

    thanks alot guy for helping me out


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    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)

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now