p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Javascript Access Database Query (http://p2p.wrox.com/showthread.php?t=33196)

joefawcett May 8th, 2008 01:55 PM

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)

gok May 8th, 2008 02:35 PM

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)

joefawcett May 8th, 2008 02:50 PM

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)

gok May 8th, 2008 03:28 PM

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)

joefawcett May 8th, 2008 04:04 PM

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)

gok May 8th, 2008 04:12 PM

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


gok May 8th, 2008 09:40 PM

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


joefawcett May 9th, 2008 02:05 AM

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)

gok May 9th, 2008 01:53 PM

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)


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

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