ASP Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreASP Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old February 3rd, 2003, 12:22 AM
bella bella is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Sydney
Posts: 33 bella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 46 sec
Reputation Power: 6
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???

Reply With Quote
  #2  
Old February 3rd, 2003, 12:50 AM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 31 m 56 sec
Reputation Power: 8
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?
__________________
El éxito consiste en una serie de pequeñas victorias día a día

MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html

Reply With Quote
  #3  
Old February 3rd, 2003, 04:06 PM
bella bella is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Sydney
Posts: 33 bella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 46 sec
Reputation Power: 6
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????

Reply With Quote
  #4  
Old February 11th, 2003, 05:22 AM
ngibsonau ngibsonau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 138 ngibsonau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.
__________________
--

ngibsonau

Reply With Quote
  #5  
Old February 12th, 2003, 12:20 AM
bella bella is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Sydney
Posts: 33 bella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 46 sec
Reputation Power: 6
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!!!!

Reply With Quote
  #6  
Old February 12th, 2003, 01:44 AM
ngibsonau ngibsonau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 138 ngibsonau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #7  
Old February 12th, 2003, 04:17 PM
bella bella is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Sydney
Posts: 33 bella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 46 sec
Reputation Power: 6
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>

Reply With Quote
  #8  
Old February 12th, 2003, 07:59 PM
ngibsonau ngibsonau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 138 ngibsonau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #9  
Old February 13th, 2003, 10:41 AM
runprn runprn is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Austin TX
Posts: 2 runprn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to runprn
What if array is dependent on a query?

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.

Reply With Quote
  #10  
Old February 13th, 2003, 06:56 PM
ngibsonau ngibsonau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 138 ngibsonau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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/progra...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.

Last edited by ngibsonau : February 13th, 2003 at 06:58 PM.

Reply With Quote
  #11  
Old February 17th, 2003, 02:00 AM
yassoor's Avatar
yassoor yassoor is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada - Egypt
Posts: 60 yassoor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
newlines!! Javascript though

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
__________________
I hope this is of any help to anyone.

Yassoor
http://www.WebsitesCreation.ca

Reply With Quote
  #12  
Old April 16th, 2003, 11:22 AM
ewillyb ewillyb is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 1 ewillyb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
accessing server-side array from client JS

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreASP Programming > Sharing data between Client and Server Scripts


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap&