Solved

JavaScript array to VBscript

Posted on 2001-09-10
13
719 Views
Last Modified: 2010-08-05
I created an array in Javascipt thru a client-side table.  Now I need to access this array is VBscript.  I can't seem to figure out how to do this.

Any help will be greatly appreciated.

0
Comment
Question by:agomen
13 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6470657
The following scripts shows how to pass a single variable from javascript to vbscript on the client side:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateHidden()
{
document.form1.hiddenfield.value = document.form1.text1.value;
alert(document.form1.hiddenfield.value);
}
//-->
</SCRIPT>

<SCRIPT LANGUAGE=vbscript >
function ShowPassed
    dim strPassedValue
    strPassedValue = document.form1.hiddenfield.value
    msgbox strPassedValue
end function
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="text" id=text1 name=text1>
<INPUT type="hidden" id=hiddenfield name=hiddenfield>
<INPUT type="button" value="JavaScript" id=button1 name=button1 onClick = "JavaScript:PopulateHidden()">
<INPUT type="button" value="VBScript" id=button2 name=button2 onClick ="ShowPassed()">
</FORM>

</BODY>
</HTML>

One could conceivable do the same thing with an array by extending the code and by using multiple hidden fields:

You could write a loop in JavaScript to populate hidden fields and then write a loop in VBScript to get the values from those fields. If you are not sure how to do this, I could post some sample code.

Fritz the Blank

0
 
LVL 19

Expert Comment

by:webwoman
ID: 6470713
You can't access anything on a page from another page unless you pass the values or save the data somewhere.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6470801
Agomen: VBScript on the client or in asp on the server?

If the latter, you need to unfold the array and pass it in either a url or a form field.

Michel
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 300 total points
ID: 6471104
Okay, here is the code with the loops. Press the JavaScript button first and then the VBScript button next to see how it works:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
var JavaArray = new Array(4)

JavaArray[0] = "a"
JavaArray[1] = "b"
JavaArray[2] = "c"
JavaArray[3] = "d"

function PopulateHidden()
{
for(i=0;i<JavaArray.length;i++)
     {
     eval("document.form1.hiddenfield" + i + ".value = JavaArray[i]");
     }
}
//-->
</SCRIPT>

<SCRIPT LANGUAGE=vbscript >
function ShowPassed
     dim strPassedValue
     for x=0 to 3
          strPassedValue = eval("document.form1.hiddenfield" & x & ".value")
          msgbox strPassedValue
     next
end function
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="text" id=text1 name=text1>
<INPUT type="hidden" id=hiddenfield0 name=hiddenfield0>
<INPUT type="hidden" id=hiddenfield1 name=hiddenfield1>
<INPUT type="hidden" id=hiddenfield2 name=hiddenfield2>
<INPUT type="hidden" id=hiddenfield3 name=hiddenfield3>

<INPUT type="button" value="JavaScript" id=button1 name=button1 onClick = "JavaScript:PopulateHidden()">
<INPUT type="button" value="VBScript" id=button2 name=button2 onClick ="ShowPassed()">
</FORM>
</BODY>
</HTML>
0
 

Author Comment

by:agomen
ID: 6475007
I understand what you are telling me to do.  My problem is that the array I am using is variable length.  I am going to have a least 200 "rows" of data.  I can't have a fixed form.

This is what I am trying to do.  Everytime I click the function update_form is called I get the following error:

document.datafile.studio0 is null or not an object

The error is on the first eval statement.  



<input type="button" value="COMPLETED" name="completed" onclick=update_form()>

function update_form()
{
  document.write ("<form name=datafile method=post
    action=update_data.asp>");
  for(var i=0;i<top.pv_array.length;i++)
  {
   document.write ("<input type=hidden name=studio+i+>");
   document.write ("<input type=hidden name=pdate+i+>");
   document.write ("<input type=hidden name=sitting+i+>");
   document.write ("<input type=hidden name=poseno+i+>");
   document.write ("<input type=hidden name=dp+i+>");
   document.write ("<input type=hidden name=staff+i+>");
   document.write ("<input type=hidden name=mem+i+>");
   document.write ("<input type=hidden name=prnt+i+>");
   document.write ("<input type=hidden name=dens+i+>");
   document.write ("<input type=hidden name=art+i+>");          
   eval("document.datafile.studio"+i+".value=
     '"+myarray[i].studio+"';);
   eval("document.datafile.pdate"+i+".value=
     '"+myarray[i].photodate+"';);
   eval("document.datafile.sitting"+i+".value=
     '"+myarray[i].setno+"';);
   eval("document.datafile.poseno"+i+".value=
     '"+myarray[i].pose+"';);
   eval("document.datafile.dp"+i+".value=
     '"+myarray[i].dp+"';);
   eval("document.datafile.staff"+i+".value=
     '"+myarray[i].staff+"';);
   eval("document.datafile.mem"+i+".value=
     '"+myarray[i].mset+"';);
   eval("document.datafile.prnt"+i+".value=
     '"+myarray[i].prnt+"';);
   eval("document.datafile.dens"+i+".value=
     '"+myarray[i].density+"';);
   eval("document.datafile.art"+i+".value=
     '"+myarray[i].art+"';);
  }
  document.write ("<input type=hidden name=cnt>");
  arraylen = top.pv_array.length;
  var cntstr =  
    document.datafile.cnt.value='"+arraylen+"';";
  eval(cntstr);
  document.write ("<input type=submit name=submit
    style=DISPLAY: none>");
  document.write ("</form>");
  var submitstr = "document.datafile.submit();";
  eval(submitstr);
}


Any ideas??
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6475043
I am not sure if this will be helpful or not, but...

Use the same idea that I suggested but with the following changes:

Just use one hidden field.

Iterate through the JavaScript array appending all of the values to one variable seperated by a distinctive character such as a "~."

Set the hidden field to that variable's value.

Use a VBScript to get the value from the hidden field and then parse the string to get the values.

It would be a good idea to include the array length as the first value to help you set up the VB loop.

If this isn't clear, please let me know.

Fritz the Blank
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6475061
Also,

For your example, I think that you need to do this:

document.write ("<input type=hidden name=studio" +i+ ">");
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6475197
You cannot do document.write after the page has loaded

try this:


fields = new Array(
"studio",
"pdate",
"sitting",
"poseno",
"dp",
"staff",
"mem",
"prnt",
"dens",
"art"
)


function update_form() {
  txt = "<form name=datafile method=post action=update_data.asp>";
 for(var i=0;i<top.pv_array.length;i++)  {
   for (j=0;j<fields.length;j++) {
     txt +='<input type=hidden name="'+fields[j]+i+'" value="'+myarray[i][fields[j]]+'">\n';
   }
 }
 txt += '<input type=hidden name=cnt value="'+top.pv_array.length+'">\n';
 txt +='</form>';
 if (document.all) {
   document.all.formDiv.innerHTML=txt;
   document.datafile.submit();
 }
 else if (document.layers) {
   with(document.layers['formDiv'].document) {
      write(txt); close();
      datafile.submit()
   }
 }
}

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6475199
and have

<div id="formDiv"
style="position:absolute; visibility:hidden"></div>

and make sure the names of the fields are the same as in the array

Michel
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6475233
Okay, here is a more generalized script that can handle a large array. The JavaScript array can be whatever you want it to be; I provided the one below just for testing purposes. The trick here is that the code concactenates all of the values in the JavaScript array and stores them to a hidden field. The VBScript grabs that value, parses the string, and then returns the values to a VBScript array.


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
var JavaArray = new Array(4)

JavaArray[0] = "a"
JavaArray[1] = "b"
JavaArray[2] = "c"
JavaArray[3] = "d"

function PopulateHidden()
{
var strArrayList = JavaArray.length
for(i=0;i<JavaArray.length;i++)
     {
     strArrayList = strArrayList + "~" + JavaArray[i];
     }
document.form1.text1.value = strArrayList + "~";
alert(document.form1.text1.value);
}
//-->
</SCRIPT>

<SCRIPT LANGUAGE=vbscript >

function ShowPassed
     dim strPassedValue, iCounter, iLocation, strRetrievedValue, iRightLength, strArrayValue, x
     dim arrVBValues(300)
     
     strPassedValue = document.form1.text1.value
     iCounter = 0
     
     do while len(strPassedValue)>1
          iLocation =inStr(1,strPassedValue,"~",0)
          iCounter = iCounter + 1
          strRetrievedValue = Left(strPassedValue,iLocation -1)
          arrVBValues(iCounter) =  strRetrievedValue
          iRightLength = len(strPassedValue) - iLocation
          strPassedValue = right(strPassedValue, iRightLength)
     loop
     
     for x=1 to iCounter
          msgbox arrVBValues(x)
     next    

end function
</SCRIPT>

</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
<INPUT type="hidden" id=text1 name=text1>
<INPUT type="button" value="JavaScript" id=button1 name=button1 onClick = "JavaScript:PopulateHidden()">
<INPUT type="button" value="VBScript" id=button2 name=button2 onClick ="ShowPassed()">
</FORM>


</BODY>
</HTML>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6475258
You have too much time on your hands, Fritz ;-)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6475414
Hey Michel,

Call it beginner's zeal. I have only been at this for a few months, so it's still a lot of fun. I find trying to answer these questions a good way to learn.

Only 693665 points, you say? Watch out, in a couple of years I might catch up to where you are now!  

Fritz the Blank
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6476043
It is the BEST way to learn - a reason why I have so many points.

Michel
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

705 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

12 Experts available now in Live!

Get 1:1 Help Now