Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 8th, 2008, 01:55 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old May 8th, 2008, 02:35 PM
gok gok is offline
Registered User
 
Join Date: May 2008
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)
 
Old May 8th, 2008, 02:50 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old May 8th, 2008, 03:28 PM
gok gok is offline
Registered User
 
Join Date: May 2008
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)
 
Old May 8th, 2008, 04:04 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old May 8th, 2008, 04:12 PM
gok gok is offline
Registered User
 
Join Date: May 2008
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?

 
Old May 8th, 2008, 09:40 PM
gok gok is offline
Registered User
 
Join Date: May 2008
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

 
Old May 9th, 2008, 02:05 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old May 9th, 2008, 01:53 PM
gok gok is offline
Registered User
 
Join Date: May 2008
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)





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 [email protected] 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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.