Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing an Javascript Array  to CGI

Posted on 1998-07-30
2
Medium Priority
?
479 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

824 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