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!

user2922456
  • 391
  • 4
  • 10
  • 24

2 Answers2

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
%>
John
  • 4,658
  • 2
  • 14
  • 23
  • 2
    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 – John Mar 30 '14 at 15:22
  • Ok...which parameter I need to send to the function..How I use the input type hidden? – user2922456 Mar 30 '14 at 15:32
  • Request.Form reads the value of the hidden field. (or any other named field) - I've made another edit – John Mar 30 '14 at 15:36
  • 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... – user2922456 Mar 30 '14 at 17:05
  • The way I wrote my example, each table row is a separate form. The `
    ` 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
    – John Mar 30 '14 at 18:45
  • Ok..I understood this..each row has its
    and still the same problem
    – user2922456 Mar 30 '14 at 19:04
  • Are you sure you've closed the form tag in your do until loop. The error you describe is exactly what I'd expect from one form with multiple inputs with the same name – John Mar 31 '14 at 00:21
  • `
    ` wrapping a `` you're kidding right? If you are going to do that at least wrap from with the `` tag that encompasses your values in a nested ``. Anything else will invalidate the HTML structure. See [HTML: Is it possible to have a FORM tag in each TABLE ROW in a XHTML valid way?](http://stackoverflow.com/a/1249715/692942)
    – user692942 Mar 31 '14 at 10:50
  • @Lankymart Yes, mea culpa, I was trying to demonstrate the concept of posting data to the server in a form, vis a vis trying to execute a function as you would with client side js, and I wasn't paying enough attention to the html. – John Mar 31 '14 at 22:46
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.