0

I create a dynamic table in asp classic for each row there is a button, i need that this button will active some function..

In some reason the button dosent react..

The source code:

 <%
set con = Server.CreateObject("ADODB.Connection")
con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
set rs = con.Execute("Select ProdID,ProductName,BrandName,CategoryName,Description,Price FROM Products P,Brands B,Categories C WHERE P.mode = true AND InStock=true AND P.CatID =C.CatID AND SupID = BrandID")
Dim filePath  
Dim Id
Dim nNumOfItems 
Dim NumStr
 nNumOfItems = 0
Do Until rs.EOF
nNumOfItems = nNumOfItems + 1
Id = rs.Fields("ProdID").Value
Response.Write "<tr>"
Response.Write "<td>"
Response.Write rs.Fields("ProductName").Value
Response.Write "</td>"
Response.Write "<td>"
Response.Write "<button  name = ' DeleteProduct '  value ='DeleteProduct' class = 'button' onClick='DeleteProduct ('Id')'>"
Response.Write "Delete Product"
Response.Write "</button>"
Response.Write "</td>"
Response.Write "</tr>"
rs.MoveNext
     Loop

    rs.Close
  Set rs = Nothing  
   con.Close
       Set con = Nothing
   %>

   <%
  Function DeleteProduct(Id)
  set con = Server.CreateObject("ADODB.Connection")
  con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
  set rs= "Update products SET mode = false WHERE ProdId= "&cint(Id)&""
  response.write("<script language=""javascript"">alert('Product has been removed!');      </script>") 

  End Function
  %>

Thanks for help!

2 Answers 2

3

Classic ASP is server side code, your button needs to be be a submit input which posts a form to the server

Do Until rs.EOF
nNumOfItems = nNumOfItems + 1 %>
<form method="post">
<input type="hidden" name="id" value="<%=rs.Fields("ProdID").Value %>">
<tr>
<td>
<%= rs.Fields("ProductName").Value %>
</td>
<td>
<input type="submit" name="DeleteProduct" Value="Delete Product">
</td>
</tr>
</form>
<% rs.MoveNext
     Loop

Edit - here's how to do the database query. This code should go at the start of your page, before any html. The conditional (if) statement checks to see if there has been a form submission. If it has then the code is executed

<%
If Request.Form("DeleteProduct") <>"" then
  set con = Server.CreateObject("ADODB.Connection")
  con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
  con.Execute ("Update products SET mode = false WHERE ProdId="& cint(Request.Form("Id"))
  response.write("<script language=""javascript"">alert('Product has been removed!');      </script>") 
End If
%>
Sign up to request clarification or add additional context in comments.

9 Comments

I've rewritten your code to use a form submit. Also it's much easier to close your asp block and just use HTML where necessary than to use repeated Response.Write statements. Your database code also needs a few changes which I'll show you in another edit
Ok...which parameter I need to send to the function..How I use the input type hidden?
Request.Form reads the value of the hidden field. (or any other named field) - I've made another edit
In some reason it works only for one product in the table..if I have more than one it just sends the ID'S of all the product to the query and ofcourse it collapsed...
The way I wrote my example, each table row is a separate form. The <form> tags are inside the do until loop so it should only send one one id in the form collection. Funkavenger's method in the other answer using links is indeed lighter and simpler than my solution. I was trying to provide a solution which is as close to your own code as possible, so as to help you understand how server side code works
|
2

If you just want to have a way to loop products and delete items one at a time, I would use a link by each item. It's lighter, simpler code and you can stylize the links.

Here's your code using links instead of form buttons:

<%
set con = Server.CreateObject("ADODB.Connection")
con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
set rs = con.Execute("Select ProdID,ProductName,BrandName,CategoryName,Description,Price FROM Products P,Brands B,Categories C WHERE P.mode = true AND InStock=true AND P.CatID =C.CatID AND SupID = BrandID")
Dim filePath  
Dim Id
Dim nNumOfItems 
Dim NumStr
 nNumOfItems = 0
Do Until rs.EOF
nNumOfItems = nNumOfItems + 1
Id = rs.Fields("ProdID").Value
Response.Write "<tr>"
Response.Write "<td>"
Response.Write rs.Fields("ProductName").Value
Response.Write "</td>"
Response.Write "<td>"

Response.Write "<a href='DeleteProduct.asp?ID='" & ID & "'>"
Response.Write "Delete Product"
Response.Write "</a>"

Response.Write "</td>"
Response.Write "</tr>"
rs.MoveNext
     Loop

    rs.Close
  Set rs = Nothing  
   con.Close
       Set con = Nothing
   %>

DeleteProduct.asp:

<%
ID = Request("ID") 

'Verify that this is in the format that you're expecting like isNumeric(ID) if it's a number

  set con = Server.CreateObject("ADODB.Connection")
  con.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("WebData/DB.mdb") & ";"
  con.Execute ("Update products SET mode = false WHERE ProdId=" & ID)
  response.write("<script language=""javascript"">alert('Product has been removed!');      </script>") 
%>

Additional Notes:

  1. If you want to allow the user to delete more than one product at a time, use the form code you've started with, but put checkboxes next to each line item with one button to "Delete All Checked".
  2. When you start using SQL server, you should do this in a stored procedure to control permissions and avoid SQL injection attacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.