JavaScript array to VBscript

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.

agomenAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
fritz_the_blankConnect With a Mentor Commented:
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
 
fritz_the_blankCommented:
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
 
webwomanCommented:
You can't access anything on a page from another page unless you pass the values or save the data somewhere.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Michel PlungjanIT ExpertCommented:
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
 
agomenAuthor Commented:
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
 
fritz_the_blankCommented:
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
 
fritz_the_blankCommented:
Also,

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

document.write ("<input type=hidden name=studio" +i+ ">");
0
 
Michel PlungjanIT ExpertCommented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
fritz_the_blankCommented:
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
 
Michel PlungjanIT ExpertCommented:
You have too much time on your hands, Fritz ;-)
0
 
fritz_the_blankCommented:
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
 
Michel PlungjanIT ExpertCommented:
It is the BEST way to learn - a reason why I have so many points.

Michel
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.