Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass information between two htm file by using VBScript 2

Status
Not open for further replies.

wg26

Programmer
Mar 21, 2002
135
0
0
US
Hi Everyone,

I don't know if my question should be posted here. But I thought I give it a try.

I am trying to use VBScript to develop a webpage (this is a just a way for me to learn VBScript and I never develop any webpage before, therefore, I thought I may learn them all together). My question is that in my first html file, I have a list of items that can be checked. After users have checked itmes, they can click show_itme button to see detail about the each item they have selected. Here I used a Sub showitem_onClick() to open new document(document.open()) and use (document.write()) to write some html codes. And items are displayed OK. But now I want to open another html file, and display the exact same things and with more stuffs in the page. I don't know how I can pass this checked items to this new html page by using VBScript or Html code. Do I need to create a object to do this? Can anyone kindly give me some suggestions about what I should do? (to make my question clear, it is more like, when you buy stuff from website, you select item and add it to cart, when you click on Checkout button, there is new page opened and the item you selected is in the page. my question is how you pass this select item from one page to another page by using VBScript) Sorry for this long post and Thanks a lot for any help.
 
You "call" the new page by setting
Code:
document.location.href
. Pass your parameters to the new page via the search (query) string:
Code:
document.location.href = "page2.htm?p1=" & strP1 & ";p2=" & strP2
Then in your second page, you use
Code:
document.location.search
to obtain the values passed. You'll need to parse these out of the result string. Don't forget to URLDecode the string before trying to use it.

You can encode your parameter list almost any old way that makes sense. If you had 10 items you could use:
Code:
page2.htm?YYNNNYNYNY
... for example. You do not need to use "name=value" syntax unless it makes sense to do so.
 
Hi Dilettante, Thanks for your help.

Can you take a look the code and see what is wrong? I kept getting error message at line: document.location.href ="MyPaymentPage.htm?theName(index) =" &Name(index) ";theValue(index) =" &Value(index). The error message is: expected end of statement...

here is the whole code:

Dim theItem, totalNum, theBoxNum, Index
Dim Name(11), Value(11)

theItem = document.ProductForm
totalNum = 0

for theBoxNum=0 to ProductForm.elements.length-2
if ProductForm.elements(theBoxNum).checked then
totalNum = totalNum+1
Name(Index) = ProductForm.elements(Index).name
value(Index)= ProductForm.elements(Index).value
end if
next

if totalNum<>0 then
document.open().location.href = &quot;MyPaymentPage.htm&quot;
for index=0 to totalNum
document.location.href =&quot;MyPaymentPage.htm?theName
(index) =&quot; &Name(index) &quot;;theValue(index) =&quot; &Value(
index)
next
document.close()
else
alert &quot;You must select one or more items.&quot;
end if
end sub
Once again, thank you for your help. I really appreciate it.
 
> totalNum = totalNum+1
> Name(Index) = ProductForm.elements(Index).name
> value(Index)= ProductForm.elements(Index).value
Index is not yet valuable. Should be:
totalNum = totalNum+1
Name(totalNum) = ProductForm.elements(theBoxNum).name
value(totalNum)= ProductForm.elements(theBoxNum).value
> for index=0 to totalNum
> document.location.href =&quot;MyPaymentPage.htm?theName
> (index) =&quot; &Name(index) &quot;;theValue(index) =&quot; &Value(
> index)
> next
This for loop will iterate totalNum+1 times and a 1 is missing.
Try something like this:
For index=1 to totalNum
document.location.href =&quot;MyPaymentPage.htm?theName(index)=&quot; & Name(index) & &quot;;theValue(index)=&quot; & Value(index)
next

Hope This Help
PH.
 
Thanks PH., I have tried your way but still get the same error message, I think there is something wrong about syntax of that line of code...I am digging into it now and hopefully I can find something. Thanks for your help. Really appreciate it.
 
Have you corrected the missing & ?

Hope This Help
PH.
 
As for the first issue, I agree with PHV that you goofed on the subscript expression in your
Code:
for theBoxNum
loop.

There was also a missing concatenation operator (&) as pointed out.

But your &quot;href building&quot; loop is a problem too. For one thing you can only set it once, not repeatedly as shown. Setting the href in this manner immediately redirects the browser to the specified page.

Perhaps you are looking for something more like:
Code:
Dim strQuery

If totalNum <> 0 Then
  strQuery = &quot;?&quot;
  For index=0 To totalNum
    strQuery = strQuery & Name(index) & &quot;=&quot; & Value(index) & &quot;;&quot;
  Next
  strQuery = Left(strQuery, Len(strQuery) - 1) 'Remove last ;
  document.location.href = &quot;MyPaymentPage.htm&quot; & strQuery
Else
  alert &quot;You must select one or more items.&quot;
End If
No document.open/.close is needed here at all.

I'm also concerned about the use of the form's
Code:
elements
collection, which will pick up more than just the checkboxes. I see you addressed this using
Code:
.elements.length - 2
. It is generally better to assign a group of related elements a common
Code:
id
attribute value and access the resulting named collection:
Code:
   :
<input type=checkbox id=chkProduct>Comb<br>
<input type=checkbox id=chkProduct>Razor<br>
<input type=checkbox id=chkProduct>Toothbrush<br>
   :
Then your statement:
Code:
Name(totalNum) = ProductForm.elements(theBoxNum).name
Becomes:
Code:
Name(totalNum) = ProductForm.chkProduct(theBoxNum).name

But now we get into another messy area. The
Code:
name
attribute is really only meant to &quot;tag&quot; form elements to identify them for a form submission to the server. You won't be doing that here at all. You can get lucky here, because in most situations Internet Explorer will coerce the
Code:
id
value to be equal to the specified
Code:
name
value when the author neglects to specify an
Code:
id
. You should not count on this though.

Typically to avoid trouble, HTML authors who plan to use client-side script should be careful to specify both the
Code:
name
and
Code:
id
attributes, giving them identical values if desired. But for your purpose here you need only the
Code:
id
and not the
Code:
name
because you are only using it within your client-side script.

Of course you are using the
Code:
name
attribute's value to build your query string. You might want to reconsider this as well.

The problem is that you will need to parse this monster apart in your next page. Do you really want to or need to deal with parsing &quot;name=value;name=value;...&quot; syntax? For a simply array of values? I think not.

I would eliminate using the &quot;name=&quot; syntax. I'd used just &quot;value;value;value;...&quot; instead if this is really just a list of values to be treated similarly by the next page loaded.

I'm not so sure about the
Code:
value
attribute either. Are you sure this will carry the values you want to pass to the next page? What do these &quot;values&quot; look like? Integers? File names? URLs???

You may be ok assigning them to the checkboxs'
Code:
value
attributes as long as they aren't too lengthy or &quot;strange&quot; (URLs can be a nuisance, but you may get away with it).
Code:
   :
<input type=checkbox id=chkProduct value=&quot;Comb&quot;>Comb<br>
<input type=checkbox id=chkProduct value=&quot;Razor&quot;>Razor<br>
<input type=checkbox id=chkProduct value=&quot;Toothbrush&quot;>Toothbrush<br>
   :
But at this point things readily collapse down to one loop, and you don't need those arrays:
Code:
Dim strQuery, lngTotal, lngIndex

strQuery = &quot;?&quot;
lngTotal = 0
With ProductForm
  For lngIndex = 0 To .chkProduct.length - 1
    If .chkProduct.checked Then
      lngTotal = lngTotal + 1
      strQuery = strQuery & .chkProduct(lngIndex).value & &quot;;&quot;
    End If
  Next
End With
If lngTotal > 0 Then
  strQuery = Left(strQuery, Len(strQuery) - 1) 'Remove last ;
  document.location.href = &quot;MyPaymentPage.htm&quot; & strQuery
Else
  alert &quot;You must select one or more items.&quot;
End If
I haven't verified the code above though, it was only typed &quot;freehand&quot; and you'd need to test it. It looks ok right now though.

In your second page (the &quot;MyPaymentsPage.htm&quot;) you can just obtain the string from
Code:
document.location.search
. But verify what you get back, I seem to recall that it returns the &quot;?&quot; as the first character. If so, you'll need to trim it off the string, and then you can use
Code:
Split()
to break up your values.


Well, those are my two cents on the matter. Good luck.
 
Thank you so much, dilettante, for you help. I would give you another star if I could.

Most parts of my script is working now. The only problem I have now is that in my second page, I used the following code to get the string, which is pass over by using code:
document.location.href = &quot;MyPaymentPage.htm&quot; & strQuery in the first page.

Dim strFromPageOne
strFromPageOne = document.location.search
strFromPageOne = Mid(strFromPageOne,1) 'to remove ?

Dim eachItem, ItemValue, Price
eachItem = split(strFromPageOne, &quot;;&quot;)

'Then I use loop to get each item's Value, which is the
'price of each item

for i=0 to UBound(eachItem)-1
Price = eachItem(i).value
some statement to use Price
Next

But when I am trying to run the script, eachItem seems empty. I don't know if I used document.location.search correctly or anything else is wrong. Would you please give me some suggestions? Thank you so much and I really appreciate your generous help...

 
I have just figured out what is wrong with my code. I should use eachItem(i) instead of eachItem(i).value. It is working now. Anyway, thank you very much for you help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top