asp code... can anybody help me?

clarkey boy

New Member
Hi,

I have only been learning ASP for several days now and am hoping to become a professional web designer.

I used the following code for my page - it does not come up with any errors and I used basically the same code for another page (which does work), but this page does not work as it will not submit comments from the form into the database. I would be very grateful for any help anyone could give me.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Comments</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<% 
Dim adoCon
Dim rscomments
Dim SQL
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Mode=admodesharedenynone
adoCon.Open ("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=d:\websites/clarkeyboy/datasource/comments.mdb")
Set rscomments = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT comments FROM comments"
rscomments.CursorType = 2
rscomments.LockType = 3
rscomments.Open SQL, adoCon
%>
<form>
<strong>
Comments: (Place your comments here and press enter)
</strong>
<input type="text" rows="20" cols="150" name="comment">
</form>
<table align="center" border="3" width="100%">
<tr>
<td width="10%">
<b>
<center>
Comment Number
</center>
</b>
</td>
<td width="90%">
<b>
<center>
Comments
</center>
</b>
</td>
</tr>
<%
Dim x
x = 0
Do While not rscomments.EOF
%>
<tr>
<td width="50%">
<%
x = x + 1
Response.Write(x)
%>
</td>
<td width="50%">
<%
Response.Write (rscomments("comments"))
%>
</td>
</tr>
<%
rscomments.MoveNext
Loop
%>
</table>
<%
Dim strcomment
strcomment = Request.Form("comment")
Dim strNothing
strNothing = ""
If strcomment = strNothing Then
Response.Write("Please submit a comment")
Else
rscomments.addnew
rscomments("comments") = strComment
rscomments.update
Response.Redirect"comments.asp"
Response.Write("Thank you for submitting a comment")
End If
rscomments.Close
%>
</body>
</html>

Displaying the information from the database works fine - so the connection is definitely there - but I can't get it to create new records.

I have a database called comments, a table called comments and the column being taken from the table is also called comments - not necessarily a very good combination to call them all comments but its easy to handle at this stage.
 

Arkette

New Member
Two things to say really: First of all, although asp will no doubt continue to be supported for a very long time to come, if you're just starting to learn this language, then I would seriously suggest that you switch immediately to asp.net. ASP.net is the latest version of ASP and a very much more powerful and versatile programming language for the web.
Secondly you might find that you have more luck with DSNless connections in legacy ASP if you use the following connection string form when connecting to msaccess dBases.

Code:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= mydBase.mdb;Jet OLEDB:Database Password=XXXXX"
 

clarkey boy

New Member
I actually found out the problem today - I missed out the method="post" from the form so it would not submit. I am way more advanced now that I know how that works. My next problem is that I have a loop on a products page where it puts products and product information from the database to the web page in a table. There is a textbox in each row which has a variable name - the name is the same as the productID in the database. I want to be able to take the number in the textbox and take that away from the stock count for the products in the same rows as the text boxes where values were submitted... but can not find a way to get the value of the textbox out because of the name being <%x%> where x represents rsproducts("productID")... Just have to use trial and error and learn from my mistakes!

RC
 

clarkey boy

New Member
ok... its a pretty long code. This is the entire script - I have annotated some of it too.

Page Information
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Admin Comments Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
Body Information
Code:
<body bgcolor="#990000" style="color:#FFFFFF"><center><font size="4"><b><i>Richard Clarkes' Website; created on 21st January 2006.To go back to my homepage please click <a href="index.htm">Here</a>!</i></b></font></center>
The Database Connection
Code:
<% 
Dim adoCon, rsproducts, SQL, x, product, productid, stock, y, z
Set adoCon = Server.CreateObject("ADODB.Connection")
Set rsproducts = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT product, productid, stock, outgoing FROM products"
adoCon.Mode=admodesharedenynone
adoCon.Open ("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=d:\websites/clarkeyboy/datasource/order1.mdb")
rsproducts.CursorType = 2
rsproducts.LockType = 3
rsproducts.Open SQL, adoCon
%>
The Table Headers
Code:
<table width="100%"><tr>
<td><strong>Products</strong></td>
<td><strong>Product ID</strong></td>
<td><strong>In Stock</strong></td>
<td><strong>Outgoing</strong></td>
</tr>
Do loop for while there are products left - this writes each required column from the database into the table under the relevant heading.
Code:
<%
Do While not rsproducts.EOF
%>
<tr>
<td><%Response.write(rsproducts("product"))%></td>
<td><%Response.write(rsproducts("productid"))%></td>
<td><%Response.write(rsproducts("stock"))%></td>
<td><%Response.write(rsproducts("outgoing"))%></td>
<%x = rsproducts("productid")%>
<form action="adminorder.asp" method="post">
<td><input type="text" name="<%=x%>" value="3"></td>

Note: x is a variable which, within this loop, becomes the same as the productID of the product in each row. x is, in effect, the name of the text box - so each textbox is called the productID of that row - the productID being specified by the administrator when the product is added to the list.

The code below is some of the attempts I made at solving the problem.

Code:
<td><%y = "=request.form(""" & x & """)"%></td>
</tr>
<%
'y = x & "= Request.Form(""" & x & """)"
'x = Request.querystring(""" & x & """)
'rsproducts("stock") = rsproducts("stock") + 20'
'y = "Request.Form(""" & x & """)"
'rsproducts("stock") = rsproducts("stock") - y
rsproducts.update
rsproducts.movenext
loop
%>
Form for creating a product, as well as entering its productID and stock levels.
Code:
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td><input type="submit" name="submit"></td>
</form>
</table>
<form action="adminorder.asp" method="post">
<table width="50%"><tr>
<td>Product:</td>
<td><input type="text" name="product"></td>
</tr><tr><td>Product ID:</td>
<td><input type="text" name="productid"></td>
</tr><tr>
<td>Stock:</td>
<td><input type="text" name="stock"></td>
</tr><tr>
<td></td>
<td><input type="Submit" name="submit"></td>
</tr>
</table>
Enters the information from the form into the database and then refreshes the page so as to update the list of products.
Code:
<%
product = request.form("product")
productid = request.form("productid")
stock = request.form("stock")
If product = "" then
Else
If productid = "" then
Else
If stock = "" then
Else
rsproducts.addnew
rsproducts("product") = product
rsproducts("productid") = productid
rsproducts("stock") = stock
rsproducts.update
response.redirect("adminorder.asp")
End If
End If
End if
%>
</body>
</html>

I have the add product code working properly (I think) but its the textboxes that I would like to call - but I don't know how to as they vary from one to the next according to the database column "productID".
 

thrive

New Member
Where do you want to store the value you're getting? I need to know the target of the value.

Let's say it's target_<%=%>

<td>
<form action="adminorder.asp" method="post" name="form_<%=x%>">
<input type="text" name="<%=x%>" value="3" onchange="document.form_<%=x%>.target_<%=x%>.value = document.form_<%=x%>.<%=x%>.value;">
<input type="text" name="target_<%=x%>" value="3">
</form>
<script type="text/javascript">
document.form_<%=x%>.target_<%=x%>.value = document.form_<%=x%>.<%=x%>.value;
</script>
</td>
 

clarkey boy

New Member
So how would that code be applied to the document so that it would take away the value in the relevant textbox from the stock value and also add it to the outgoing value?

By the way... I decided to give up on attempting to solve it through ASP so I tried PHP instead and, I think, I am getting on fairly well with it. I have created about 10 pages in PHP over the last 2 days - including a sitemap and an admin sitemap so that I can add entries (by putting the target address and description into 2 text boxes and then putting in the code <a href=".$target->value.">".$description->value."</a>". Its quite a clever way to create a site map with administration. I can add, delete and search entries from the same page.

RC
 
Top