...

View Full Version : JS and ASP



pinkcat_02
02-19-2003, 09:56 PM
I wonder if I can put any ASP within an javascript something like:

var marqueecontent='<nobr><%

Response.write("<font-family: Arial Narrow; font-size: 8pt>" )
Set conn = server.createobject("adodb.connection")
dbName="deneme.mdb"
connect ="provider=microsoft.jet.oledb.4.0;data source="
connect=connect & server.mappath(dbName)& ";"
conn.open connect


decribing the content dynamically by getting the data from the database?

whammy
02-20-2003, 12:02 AM
Sure, I do it all the time. Of course, you have to insert the database info into your JavaScript on the server-side.

:)

pinkcat_02
02-20-2003, 12:11 AM
hmmm that's good news but I think I need more explanation then that.

Actually I wanna do this text scroller dynamically and it's where I am still working on. So basically the javascript is consists of

describing the variables first and then using these variables in functions.

so it was the decribing the marquee's content where I used ASP to call the content from the database with the code I had shown in the previous message.

Is that enough or do I need to indicate that in the functions where it needs the marquee content? What I was thinking is that once the content is described it can be used in the functions without calling it from the database again.

Am I in the wrong direction?

whammy
02-20-2003, 12:16 AM
No, you're on the right path. Just transfer all of your database information into corresponding arrays or whatever in javascript.

Easiest way to figure it out is just to try it! Mess with some simple code first, like this:



<%@ Page Language="VBScript" %>
<%
Dim mystring
mystring = "Yay, this works!"
' This is an example
%>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
<!--
alert('<% = mystring %>');
// -->
</script>
</body>
</html>


Then move on to inserting stuff in an array, etc. If you have a problem, then your best bet to fix syntax errors in THIS case would be to view the source of the HTML that is sent to the client-side, so you can see what you're putting into the javascript... :D

pinkcat_02
02-20-2003, 12:57 AM
excellent it works with mystring bit now I'll try to replace it with the actual ASP code which will retrieve from the database.

I wonder if I can use:

set content = "SELECT * from News"

as an SQL statement in my ASP code.

Thanks very much :thumbsup:

whammy
02-20-2003, 01:13 AM
Like I said, don't wonder... try it out!

If it doesn't work, then view the source code to see what errors are being generated.

I've been programming for only about 2 years, and I've learned that probably the most important thing that a programmer needs to learn is how to research things (even by doing a google search!), and more importantly, just to keep experimenting with code to see if it works. You learn a lot that way.

Not to mention... that's what really makes programming fun, at least to me. Once you get something working, you have learned quite a few things. They all add up, too. Programming is one field that gives you a great sense of accomplishment.

:)

pinkcat_02
02-20-2003, 01:15 AM
ok i won't wonder any more :o

i am trying to see what happens...

cheers:thumbsup:

whammy
02-20-2003, 01:18 AM
Cheers to you too... now remember, if you're having specific problems with your syntax, or problems with a concept, don't forget to post here. :)

But the main thing I was trying to get across is not to ask questions like "will this work?", since the best way to find out whether something works or not is to try it first. :)

pinkcat_02
02-20-2003, 02:09 AM
well unfortunately I have tried few things and I couldn't work it out where I am doing the mistake. I have defined the ASP part as :

<%

Dim oConn
Dim oRS
Dim sSQL

Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\anticus\db\deneme.mdb"))

sSQL = "SELECT * from Branch where Branch_Code='Bristol' "
Set oRS = oConn.Execute(sSQL)

oConn.Close
Set oRS = Nothing
Set oConn = Nothing

%>


and I have used :

1) <%Response.Write( & oRS("Branch_Code").Value &)%> where it gave error about & things

2) < % = oRS("Branch_Code") %> where it didn't work

I couldn't work it out in both cases and I dunno which syntax I have to use in my javascript to display the result of my SQL statement I have included at the top
:confused:

glenngv
02-20-2003, 03:06 AM
that's because you closed the recordset and connection objects just after you created them and before you tried to retrieve of the fields in the database. Plus you have syntax errors.

<%
Dim oConn
Dim oRS
Dim sSQL

Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("\anticus\db\deneme.mdb"))

sSQL = "SELECT * from Branch where Branch_Code='Bristol' "
Set oRS = oConn.Execute(sSQL)
%>

<script language="javascript">
var branchCode = "<%=oRS("Branch_Code")%>";
alert(branchCode);
</script>

<%
oRS.close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing

%>

pinkcat_02
02-20-2003, 12:51 PM
i was thinking of using the javascript in the body part of my code. In that case I won't be able to close the oRS in the body part.

In the previous code the retrieving part of ASP was in head part and the javascript is in the body part so how can I close that in body part of the code?

Thanks

Roelf
02-20-2003, 12:55 PM
you can close it where ever you want to close it after use, you can even do:

</body>
<%
oRS.close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
%>

remember, the output of the asp code's response.write's appear in the html code which is sent to the browser. if you have asp-code, which doesnt produce html output, it can be put at the end of the file, or whereever you want to put it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum