Link to home
Start Free TrialLog in
Avatar of agomen
agomen

asked on

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.

Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

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

Avatar of webwoman
webwoman

You can't access anything on a page from another page unless you pass the values or save the data somewhere.
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
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of agomen

ASKER

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??
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
Also,

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

document.write ("<input type=hidden name=studio" +i+ ">");
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()
   }
 }
}

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
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>
You have too much time on your hands, Fritz ;-)
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
It is the BEST way to learn - a reason why I have so many points.

Michel