Helpful Information
 
 
Category: ASP Programming
Sharing data between Client and Server Scripts

Suppose I load data from a database and store it in a VBScript array using asp (i.e. this is all done from the server)

I then want to have some client side scripts (they have to be client side because they are activated depending on what the user does on the page). Is there any way of accessing the array generated at the server, through the client side scripts???

If not, how else can I write my page?? The data is loaded from a database, which is obviously done at the server. If not by doing what I have already described, how else can i use the database data in my client side scripts???

Have you considered having each client access the database directly?
...
What database are you attempting to connect to?
...
Are you familiar with database connection and ASP?

I am using an SQL Server 2000 database. I am connecting to it using ASP. What do you mean by having "each client access the database directly"?

The Server.CreateObject("ADODB.Connection") etc. lines create the connection from the server when the page is generated correct???? So then when the server has finished making the page and sends it back to the client, the connection to the database would be closed yeah???? But within the page, I copy data from the recordset into an array....SO, would this array then be able to be accessed from a client-side procedure such as formSubmit_onClick????

The answer to your question is yes.
But thats not what you want to do, and it would make your application very platform specific.

You really have three options
1. You can output your asp array into javascript if you don't care if the user can see the whole array. Here is an example of a server side variable being turned into a client side one.

eg
<%
Dim x
x = 7
%>

<html>
<head>
<script language="javascript">
var x = <%= x %>;

</script>
</head>
<body>

<a onClick="javascript:alert(x);">click me</a>
</html>

you can do the same sort of thing for the whole array or

2. You can hold off on you database query until all the input has turned up and then do the query.

ie.
display choice page on client side.
user selected choice (ie. button is submitted on a form)
the users input is stored in a variabe ( input = Request("input")
)
do the query
display different output depending on what the value of input is.

3. You are currently doing it backwards.

you do the query
store the results of the query (in a Session variable)
ask the user for input
then you display the results depending on the contents of the session variable and the user input.

There is really no need to do option 3, all you are doing is wating memory holding the query results for no real gain.
You only want to do option 1 if you dont want to submit the page in between the users choice and the display of the results.
And you can only do option 1 if the query results in the array are not to be kept secret.

Since i first posted this message and have been working on my application, option 1 was essentially what I implemented, but I'm using a VBScript array rather than Javascript, which apart from the browser dependency, shouldn't make a great deal of difference...

But I am now having problems accessed the array from the client side. I create the client side array in a script section in the head of my page, below which I have several subprograms. However I am now finding that the subprograms don't seem to be able to access the array. Why is this??? For example, suppose the value of myarray(1) is "me", my subprogram seems to think it is null. Whats going on??? Help me please!!!!

The following code will output 6 purely from the client side

<html>
<head>
<title></title>
<script language="VBscript">
Dim arr
arr = Array(4,5,6)
</script>
</head>
<body>
<script language="VBscript">
sub fred
document.write arr(2)
End sub

fred()
</script>
</body>
</html>



just place your ASP values in like this
<%
Dim arr
arr = Array(4,5,6)
%>
<html>
<head>
<script language="VBscript">
arr = Array(<%
For i = 0 to 2
response.write arr(i)
if(i < 2) then response.write ","
Next
%>)
<script>
</head>
<body>
<script language="VBscript">
sub fred
document.write arr(2)
End sub

fred()
</script>
</body>
</html>

And now their off the server and on the client side

yes, i understand how to make a client side array out of a server side array - i have done it the same as you have shown

My problem is, that I don't call the sub from within the script section, because the sub is called as a result of an onChange event within my form, so it is called from the html part of my code, not from within the script, and for some reason, despite creating the array as you have demonstrated, the sub does not detect any values in the array

I have included the code below. Two points to note - firstly, 'conn' is created in the included header file, but this isn't the problem because i've done the same thing on other pages and they work fine. Secondly, the script is longer as it has more asp/script code mixed for my task menu, but i've omitted it to simplify what is shown as it depends on the projects loaded and since this is not done correctly, tasks are unrelated to the current problem.

<% @Language=VBScript %>
<% Option Explicit %>
<% Response.Buffer = true %>
<!-- #include file="includes/validate.asp" -->

<html>
<head><title>View Task Details</title></head>

<body>

<script language=VBScript>

<% Dim projrs
Set projrs = server.CreateObject("ADODB.Recordset")
Set projrs = conn.Execute("select distinct ProjectName from Project, Task where Project.ProjectCode=Task.ProjectCode Order By ProjectName")
projrs.MoveFirst %>

Dim numprojs
Dim projnames(1)
' Reads project names into a project array
<% Dim pcount
pcount = 0
Do while not projrs.EOF %>
ReDim Preserve projnames(<%=pcount+1%>)
projnames(<%=pcount%>) = "<%=projrs.Fields("ProjectName")%>"
<% projrs.MoveNext
pcount = pcount + 1 %>
<% Loop %>
numprojs=<%=pcount%>
<% ' Close all connections and references to objects
projrs.close
Set projrs=nothing
conn.close
Set conn=nothing %>

Sub projects()
Dim taskform
Set taskform = document.Forms("tasks_readonly")

' Creates a new empty select option
Dim newOp
taskform.project.Length = 0
Dim i, name

For i = 0 to <%=pcount%>
Set newOp = document.CreateElement("OPTION")
If i=0 Then
name = "Please select a project..."
newOp.Value = ""
Else
name = projnames(i-1)
newOp.Value = name
End If
newOp.Text = name
taskform.project.add newOp
Set newOp = nothing
Next
Set taskform = nothing
End Sub

Sub project_onChange
Dim changeform, projindex
Set changeform = document.Forms("tasks_readonly")
projindex = changeform.project.SelectedIndex
If projindex = 0 Then
MsgBox "You must select a project before you can select a task.", vbOKOnly
Else
' Fill the task dropdown with task names associated with the selected project
projindex = projindex-1
Dim numoptions
numoptions = numtasks(projindex)

' Creates a new empty select option
Dim newOp
changeform.task.Length=0
Dim i, name

For i = 0 to numoptions
Set newOp = document.CreateElement("OPTION")
If i=0 Then
name = "Please select a task..."
newOp.Value = ""
Else
name = tasknames(projindex, i-1)
newOp.Value = name
End If
newOp.Text = name
changeform.task.add newOp
Set newOp = nothing
Next
End If
Set changeform = nothing
End Sub

<form name="tasks_readonly">
<!-- form allows users to select a task and project, and displays corresponding details -->
<!-- all fields except the task and project dropdowns are readonly, but the info. is displayed in form fields because this allows it to change without resubmitting the form -->

<table width=100% cols="10%, 35%, * ">

<tr><td></td><td align=left>Project:</td>
<td align=left><select name="project">
<option value=''>Please select a project...</option>
</select></td></tr>
<script>projects()</script>
<tr></tr>

<tr><td></td><td align=left>Task:</td>
<td align=left><select name="task">
<option value=''>------------------------------</option>
</select></td></tr>
<tr></tr>

<tr><td></td><td align=left>Initial Hours Estimate:</td>
<td align=left><input name="estimate" type=text size=10 readonly></td></tr>
<tr></tr>

<tr><td></td><td align=left>Total Time Spent:</td>
<td align=left><input name="tt_spent" type=text size=10 readonly></td></tr>
<tr></tr>

<tr><td></td><td align=left>Hours Outstanding:</td>
<td align=left><input name="outstanding" type=text size=10 readonly></td></tr>

</table>

</form>
</body>
</html>

Your call to <script>projects()</script> should probably have language=VBscript on it

also it may only be a cut and paste problem but you seem to be missing the </script> before the form, which will cause a big problem.

You might want to try something a little simpler for proof of concept. Maybe have projects() do an alert or something first.

If that works you know where the problem is, if it doesn't you don't have to search far through a simple example.

Great thread! I've had similar problems. The only issue is that, in my case, I need to populate the pick lists from a DB query. The values of secondary pick lists are dependent upon the previous selections. Index values could vary on a daily basis, so creating static HTML selects would not work.

What I ended up doing was creating a series of sub routines (that appear as a paginated "wizard" to the end user). However, I am being asked to consolidate the multi-page process into one page.

Performing a query from the client script works fine on localhost, but on the NT server, I get permission-related alerts. They don't stop the process, but are confusing to the end user.

Just curious if you might have run into a situation like this.

Thanks.

At the beggining I said "The answer to your question is yes."

Well here is the full answer, yes you can access server side variables from the client side "without a page refresh" but it does require every client to have installed the activex component XMLHTTP from microsoft. You could of couse produce your own applet or activeX object that can do a http request on it own, but microsofts version probably has less bugs, well maybe.

Here is an article on how you can access a session variable via a http request using the component but keep the page from refreshing. This would allow for multiple pulldowns dependent on the values of each other to be able to do sql queries on the fly without refreshing the page.

http://asia.cnet.com/builder/program/web/0,39009372,39098720,00.htm

The only other alternative is to save all possible data for every possible outcome into javascript/vbscript client side arrays and query the arrays as if they were your database. Of couse this may make you html rather large for very large arrays.

bella,

I had a similar problem but I was using javascript for Client side script.

The main problem came from the values in the database. It included chr(13) & chr(10) values .. the "New line character".

this turned
array(1) = "hello world"
to be
array(1) = "hello
world"

which was caused problems.

Open the source code in the browser and check it out
It might be something similar.!!

It would be very helpful if you give us a sample of the source that doesn't work .. I don't mean the asp .. I mean the actual page that the client recieves.

Hope this is of any help

My question is almost identical to Bella's original question.

I must access a server-side array from the client. The client code HAS to be done in JavaScript. The JS is triggered from a html element event, ie, onChange.

I have seen the simple examples of accessing server-side variables from the client. Such as:

function alertServerVar() {
alert('server var = ' + <%=serverVar%>);
}

I can do this fine for simple vars. HOWEVER, when I try to do this with an array, I get the msg: A default property was not found for the object.

Any ideas?

Please note that solutions that require me to submit to the server will not help me.

Peace,
ewillyb










privacy (GDPR)