Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #21 (permalink)  
Old May 8th, 2008, 01:55 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That looks right. Now if you've got rid of the document.write stuff, which opens a new document, you should be able to use the names array.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #22 (permalink)  
Old May 8th, 2008, 02:35 PM
gok gok is offline
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe,
I'm confused. There are no document.writeln() in .html page.
Check dbTest.htm above.
Did try to break Asp (changed login name for db connection string) but no any errors opening Htm! Looks like readDb.asp never being called!
Quote:
quote:Originally posted by joefawcett
 That looks right. Now if you've got rid of the document.write stuff, which opens a new document, you should be able to use the names array.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #23 (permalink)  
Old May 8th, 2008, 02:50 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm confused too.
Originally you wanted to use JavaScript server-side to connect to a database and use the data to populate a select list.
Now you could just create the options server-side but I thought you wanted to populate the array server-side and populate the options client-side. Is this not right?

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #24 (permalink)  
Old May 8th, 2008, 03:28 PM
gok gok is offline
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, thats right, original task was: read db records in .htm using .js.
But in this scenario I'm getting runtime error: 'Server' is undefined (for
Server.CreateObject("ADODB.Connection") call inside .js function).

As an option I was hopping to use .asp (reads db nicely):
<SCRIPT LANGUAGE="JavaScript" src="readDb.asp" runat="server"></SCRIPT>
and use .asp variables on .htm page. Evidently .asp is not
a script and there is no way to read .asp into .htm
Could you suggest any workaround to solve this puzzle?
Thanks a lot for your patient!
Gennady
Quote:
quote:Originally posted by joefawcett
 I'm confused too.
Originally you wanted to use JavaScript server-side to connect to a database and use the data to populate a select list.
Now you could just create the options server-side but I thought you wanted to populate the array server-side and populate the options client-side. Is this not right?

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #25 (permalink)  
Old May 8th, 2008, 04:04 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
<HTML><HEAD><TITLE>dbTest</TITLE>
<SCRIPT type="text/javascript" src="readDb.asp"></SCRIPT>
<script type="text/javascript">

function addNames()
{
  var list = document.getElementById("quickZoom");
  for (var i = 0, l = names.length; i < l; i++)
  {
    var oOption = new Option(names[i], names[i]);
    list.options.add(oOption);
  }
}
</script></HEAD>
<BODY onload="addNames()">
<select id="lstNames" name="quickZoom"></select>
<BODY></HTML>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #26 (permalink)  
Old May 8th, 2008, 04:12 PM
gok gok is offline
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the hints on rendering. I see array in the Head.
How to get 'names' into dbTest.htm scope from readDb.asp though?

Reply With Quote
  #27 (permalink)  
Old May 8th, 2008, 09:40 PM
gok gok is offline
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello, Joe.
After couple of hours playing with code this version works as expected:
Code:
<%@ LANGUAGE=JavaScript %>
<HTML><HEAD><TITLE>dbTest</TITLE>
<%
        // this part is running on Server side
Response.Write("<script type='text/javascript'>\n");
    var conn = Server.CreateObject("ADODB.Connection");
    var strConn = "Provider=SQLOLEDB.1;Data source=amsql;Database=pubs;Trusted_Connection=no;User ID=guest;Password=password;"
    conn.Open(strConn);
    var rs = conn.Execute("SELECT * FROM STORES");
    var i = 0;
    // store recs to the client
    Response.Write("var names=[");
    while (!rs.EOF)
    {
        if ( i++>0 ) Response.Write(",");
        var name = rs.fields("stor_name").value.toString();
        name = name.replace(/(^\s*)|(\s*$)/g, ""); // trim spaces
        Response.Write("\"" + name + "\"");
        rs.MoveNext();
    }
    Response.Write("];\n");
        rs.Close();
    conn.Close();
Response.Write("</script>");
%>
<script type='text/javascript'>
    // this part is running on Client side
    function addNames()
    {
          var list = document.getElementById("quickZoom");
          for (var i = 0, l = names.length; i < l; i++)
          {
            var oOption = new Option(names[i], names[i]);
            list.options.add(oOption);
          }
    }
</script>
</HEAD>

<BODY onload="addNames()">
Store Names:<br>
<select id="lstNames" name="quickZoom"></select>
</BODY>
</HTML>
Thanks a lot for you patience and for sharing your knowledge!
Happy programming!
Gennady

Reply With Quote
  #28 (permalink)  
Old May 9th, 2008, 02:05 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The page you showed should do it although if you are using it as an include you don't need to generate the surrounding script tags, just the JavaScript.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #29 (permalink)  
Old May 9th, 2008, 01:53 PM
gok gok is offline
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Did not get this part, Joe.
Did you mean to include DB reader like that:
Code:
<HTML><HEAD><TITLE>dbTest</TITLE>
<script type="text/javascript" language="javascript" src="readDb.asp" runat="server">
    // this part is running on Client side
    function addNames()    { ...

If so, readDb.asp is not readable: Syntax error readDb.asp, line 1
And readDb.asp:
Code:
<%@ LANGUAGE=JavaScript %>
<HTML><HEAD><TITLE>readDB</TITLE></HEAD><BODY>
<% ...
Quote:
quote:Originally posted by joefawcett
 The page you showed should do it although if you are using it as an include you don't need to generate the surrounding script tags, just the JavaScript.

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
access sql database using javascript vivek_inos Javascript How-To 2 October 12th, 2006 01:29 PM
database connection(query) within javascript funct muthumari_p2003@yahoo.com Pro JSP 0 April 5th, 2006 06:48 AM
Parameterised Access Query in javascript howbadboy Javascript 2 February 26th, 2005 12:39 PM
Access Database from Javascript with rds nosreg Access 2 February 3rd, 2005 07:19 PM



All times are GMT -4. The time now is 05:44 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.