Solved

Passing an Javascript Array  to CGI

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

856 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