Solved

Passing an Javascript Array  to CGI

Posted on 1998-07-30
2
435 Views
Last Modified: 2013-12-20
Ok. I want to pass a javascript array to a CGI. I know how to pass a series of variables, but this time I want to pass an array with multiple values. The CGI is in C running personal web server, and I am using the POST method from the HTML. I am looking for the initial cgi code if it is different than passing a few variables and the proper coding for the HTML/JavaSCript. The array is going to consist of 2 to 4 fields with a variable number of records.
ie.. from the CGI side    JavaScript side
  testArray{             pdata = new Array  
      char val1[5];      function addData(x,y,z)
      char val2[8];      {
      int arecords;        this.xData = x
      }                    this.yData = y
                           this.zData = z
                         }
             ......... Further down in the code...      
                 pData[0] = addData(value1, value2, value3)

I want to pass the pData array to the CGI. Is it possible?
Thanks in advance!
Randall
0
Comment
Question by:rcashon
2 Comments
 

Accepted Solution

by:
samasan earned 60 total points
ID: 1831282
A way to pass the array into CGI is to create the array in such a way that it is easy to parse out on the CGI side.  You concatinate the array into one, easily delimited, string.  Placing that value into an INPUT field.  The following example shows how you can add several items into an array, and how you put them in to a form easy to process in CGI.


<HTML>
<HEAD>

<script language=Javascript>

pdata = new Array(); // Allocate an unbound array
var count = 0;       // Maintain a count

/*
    addData(value1, value2, value3)
 
    It doesn't matter how many variables you pass in, as long as
    you include in the line in the function that concatinates them
    into the entry in the array.
   
    The idea is that each element in the array is made up of N values.
    So you concatinate each value into one string and enter it in the
    array.  The example below is for 3 values, it could be as many
    as you want.  So you place inbetween each value in the element in the
    array a delimiter (I use the '|' character).  At the end you
    place an element delimiter (I use the '&' character).  These
    delimiters can be anything you want, but you must make sure they
    are not going to be in the values.
*/
 
function addData(value1, value2, value3)
{
   pdata[count] = value1 + '|' + value2 + '|' + value3  + '|';
   count++;
}
addData("one", "ONE", 1);
addData("two", "TWO", 2);
addData("three", "THREE", 3);

/*
   go(input)   input is the INPUT field to place the value in.

   This function cycles thru the array and builds the one string
   that will represent the array.  The format will be:

   element1&element2&element3&...

   Each element will have the format:

   value1|value2|value3|


   For demonstration purposes, the end value will be displayed to
   the text box in the browser.  See notes to change it for use with
   CGI
*/
function go(input)
{
  input.value = "";
  for (ii = 0; ii < count; ii++)
  {
     input.value += pdata[ii] + '&';
  }

}
</script>

<BODY>
<FORM Name=ExForm> <!--You must change the form to submit into the particular function -->
<INPUT TYPE=TEXT NAME=DATA VALUE=""> <!--Change the type to be hidden -->
<INPUT TYPE=BUTTON VALUE=ClickMe OnClick="go(DATA)"
0
 
LVL 1

Author Comment

by:rcashon
ID: 1831283
samasan, Thanks that worked fine. Now I can clean up my CGI and get it going...

Randall
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

13 Experts available now in Live!

Get 1:1 Help Now