executing javascript and vbscript

G

gmail

Hi all--

I'm new to HTML and scripts and am having a problem with some code,
pasted in at the bottom. It uses javascript to get values from a form
in another frame, stores part of an SQL string in a cookie, then uses
vbscript to query a database. The page is reloaded by clicking a button
in another frame.

The problem I'm having is I have to reload the page twice (click the
button twice) before changed values are used. It seems like the
vbscript is executed before the javascript has finished, meaning the
request.cookies("WhereValue") is executed before the cookie value is set
by javascript, so the request pulls the old value. Is something like
this happening? What am I doing wrong here?

If anyone can point me in the right direction, I would appreciate it.

Thanks!
s

=== code ===
'rem -- this line reloads the page that will reflect changes (checkboxes
checked or unchecked.
'
<input type="button"
onclick="parent.frameSylvieResources.location.href='Sylvie_Resources.asp'"
value="Find these resources">


'rem -- And this is from the page being loaded
'
<html>
<script type="text/javascript">
function GetSelectedCats()
{
strSelected=""
strWhere=""
frmSelectedCats=parent.frameSylvieCategories.document.forms[0].SelCat
for (i=0;i<frmSelectedCats.length;++ i)
{
if (frmSelectedCats.checked)
{
strSelected=strSelected+frmSelectedCats.id + ", "
}
}
strSelected=strSelected.substr(0,strSelected.length-2)
if (strSelected.length > 0)
{
strWhere = "WHERE (((tblCategorys.fldCategoryID) In (" +
strSelected + "))) "
}
else
{
strWhere = "WHERE (((tblCategorys.fldCategoryID) In (0))) "
}
document.cookie="WhereValue='" + strWhere + "';"
}
</script>

<!--set up and draw the table of resources-->
<script type="text/javascript">
GetSelectedCats()
</script>
<%
strSQL=""
strWherevb=""
strWherevb=request.cookies("WhereValue")
strWherevb=mid(strWherevb,2,len(strWherevb)-2) & " "
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "Sylvie"
set rs=Server.CreateObject("ADODB.recordset")
strSQL="SELECT DISTINCT tblResources.fldResourceID,
tblResources.fldResourceTitle, tblCategorys.fldCategoryID "
strSQL=strSQL + "FROM tblResources INNER JOIN (tblCategorys INNER JOIN "
strSQL=strSQL + "tblCategorysResourcesXref ON
tblCategorys.fldCategoryID = "
strSQL=strSQL + "tblCategorysResourcesXref.fldCategoryIDref) ON
tblResources.fldResourceID = "
strSQL=strSQL + "tblCategorysResourcesXref.fldResourceIDref "
strSQL=strSQL + strWherevb + "ORDER BY tblResources.fldResourceTitle;"
rs.Open strSQL, conn
strWherevb=""
%>

<body>
<h4>Resources--select a resource to see the details.</h4>
<table border="1" width=300px>
<%do until rs.EOF%>
<tr>
<td width=10px><input type="radio" name="ResourceMarker"> </td>
<td
width=235px><%Response.Write(rs.fields("fldResourceTitle").value)%></td>
<!--probably want the get it column to say the format-->
<td width=35px>Get it</td>
<%rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>
 
D

David Dorward

gmail said:
The problem I'm having is I have to reload the page twice (click the
button twice) before changed values are used. It seems like the
vbscript is executed before the javascript has finished, meaning the
request.cookies("WhereValue") is executed before the cookie value is set
by javascript, so the request pulls the old value. Is something like
this happening? What am I doing wrong here?

Communication between the server and the client happens in one direction at
a time.

First the client makes a request, which includes any cookies that are set,
then the server makes a response (and your ASP is executed on the server),
then the response is received by the client and client side code is
executed.

So the cookie is set using your client side JavaScript AFTER all the cookies
have been sent to the server and AFTER all the server side ASP has been
executed.
 
G

gmail

David said:
Communication between the server and the client happens in one direction at
a time.

First the client makes a request, which includes any cookies that are set,
then the server makes a response (and your ASP is executed on the server),
then the response is received by the client and client side code is
executed.

So the cookie is set using your client side JavaScript AFTER all the cookies
have been sent to the server and AFTER all the server side ASP has been
executed.
Thanks for the explanation David. I thought it might have something to
do with client vs service side execution.

So is the best way to deal with something like this to use just one
language? And is javascript preferred over vbscript?

cheers!
steve
 
D

David Dorward

gmail said:
Thanks for the explanation David. I thought it might have something to
do with client vs service side execution.
So is the best way to deal with something like this to use just one
language? And is javascript preferred over vbscript?

There are some things that can be done only on the client. There are
some things that can be done only on the server. There are some things
that can be done on either (and are often best done on the server
first, and then a client side convienience method added afterwards).

On the client side, JavaScript is the only real option. The other
languages that turn up from time to time are VBScript (Internet
Explorer only) and PerlScript (browsers with an uncommon plugin only).

On the server side there are many options, and things mostly come down
to personal preference (since the client doesn't give a monkey's how
the data it recieves is generated).

Going back to your original script, it looks like you have a form, are
sucking the data out of it and putting it into a cookie, then
requesting a new page from the server. This is a long winded and
complicated way of going about things, far simpler would be to just
give the form a regular submit button and submit it (then extract the
data from the query string or post data instead of the cookie).

You also seem to be shoving the data into an SQL server without any
sanity checking - this is an open invitation for someone to inject
whatever SQL they like into your system - so you should probably do
something about that.
 
G

gmail

David said:
So is the best way to deal with something like this to use just one
language? And is javascript preferred over vbscript?

[some text snipped]

Going back to your original script, it looks like you have a form, are
sucking the data out of it and putting it into a cookie, then
requesting a new page from the server. This is a long winded and
complicated way of going about things, far simpler would be to just
give the form a regular submit button and submit it (then extract the
data from the query string or post data instead of the cookie).
I'll try that. This is the first time I've tried to do any web-related
coding, so I appreciate the pointer to something more efficient.
You also seem to be shoving the data into an SQL server without any
sanity checking - this is an open invitation for someone to inject
whatever SQL they like into your system - so you should probably do
something about that.
Ok. I'll look into that after I get basic functionality. It's only
local access at this point.

Thanks again!
steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top