Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing an Javascript Array  to CGI

Posted on 1998-07-30
2
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
samasan earned 240 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

715 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