1

I've created a webpage in asp to display a table from SQL database. I then added Buttons to each table row to update and delete each row code -

    do while not objRS.EOF

    %>

    <tr> 
        <td><%= objRS("Id") %> </td>
        <td><%= objRS("Name") %> </td>
        <td><%= objRS("Address") %></td>
        <td><%= objRS("Suburb") %></td>
        <td><%= objRS("Postcode") %></td>
        <td><%= objRS("Age") %></td>
        <td><%= objRS("Email") %></td>
        <td><Center><input type="submit" value="Update"></Center></td>
        <td><center><input type="Submit" onclick="delete(<%= objRS("Id") %>)" value="Delete"></center></td>
    </tr>
    <%
        objRS.MoveNext
        loop    
        objCon.close
    %>

also i the code to delete -

    Function delete(index)

        Dim objCon, objRS, dSQL      
        set objCon = CreateObject("ADODB.Connection")
        objCon.open "Provider=SQLOLEDB.1;Password=xxxx;Persist Security Info=True;User ID=xxxx;Initial Catalog=Customer;Data Source=PC"

        dSQL = "DELETE FROM Customer WHERE Id=" & index

        objCon.execute(dSQL)            
        objCon.close     
    End Function

I've looked everywhere but cant seem to find how to identify each different button and delete the corresponding row from the database

kurupt_89
  • 1,542
  • 8
  • 37
  • 65

2 Answers2

3

In each row have:

<a href="Page.asp?action=delete&ID=<%= objRS("Id") %>">Delete this</a>

Then the receiving page, have the code:

Dim strAction

strAction = request.querystring("action")
if(strAction = "delete")

    'Verify ID
    'Perform deletion
    'Redirect

end if

This is how you would traditionally delete it. In your example, you seem to want an AJAX function. Add this to the top of your page:

<script type="text/javascript">
    function delete(RecordID){
        alert(RecordID);
    }
</script>

That's the Javascript function you are trying to call when delete is clicked. So that would be your template to call an AJAX request to the delete script if that is the way you want to do it.

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • Thanks, im only new to this (2 days) and still getting used to it, but you and another person said I am using javascript. I'm only supposed to be using vbscript so if you can point out where I was using javascript that would be great. I was under the impression that procedure was vbscript aswell. Also, is there a way to to link the submit button with that href you provided above? – kurupt_89 May 24 '11 at 13:39
  • @Kurupt `onclick` will run Javascript code, it's a client side function called on an HTML element. You can make the function called redirect the user to the delete page as one solution if you want to use a button. Look up on google 'Javascript redirect'. – Tom Gullen May 24 '11 at 13:41
  • Thanks, makes a lot more sense now – kurupt_89 May 24 '11 at 14:11
  • I'd be wary of using a hyperlink to delete records. See this answer to [When do you use POST and when do you use GET?](http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get/46614#46614). At the bare minimum, the receiving page should display a confirmation. – Cheran Shunmugavel May 25 '11 at 05:43
  • @Cheran I usually put an `onclick="return confirm('Are you sure')"` on the hyperlink, but UI is out of scope for this question. – Tom Gullen May 25 '11 at 13:41
1

It looks like you're trying to invoke ASP code with Javascript. That ain't gonna work.

If you had your delete function in a separate ASP script, then you can create a Javascript method to handle the button clicks and call the other ASP script, either directly with a GET or with an Ajax call. Then you'd have to reload the original page.

Ed Manet
  • 3,118
  • 3
  • 20
  • 23