Solved

Passing an Javascript Array  to CGI

Posted on 1998-07-30
2
429 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

18 Experts available now in Live!

Get 1:1 Help Now