Search code examples
javascriptasp-classic

Call ASP Classic Function from Javascript


I have a file on our local database page that needs to be able to delete a client after prompting the user to confirm deletion.

The function deleteClient already exists and is very long and elaborate as there are several steps in removing the information from the database in the proper way.

I have set up the page so that when the user clicks that delete button it calls the javascript function confirmdelete() then it should call the ASP function to do the deed.

How do I call an ASP function from a javascript function?

<SCRIPT TYPE="text/javascript">
     function confirmdelete(){
         if (window.confirm('Are you sure you want to delete client?'){

                  // How do I call deleteClient here?

         }else
              return false;
     }
</SCRIPT>
<%
  Function deleteClient
    ...
    ...
  End Function
%>
<FORM NAME=addclient METHOD=post ACTION="...">
    <INPUT type=Submit name=btnSubmit value="Delete" onClick="confirmdelete()">
</FORM>

Solution

  • Yuriy's answer is correct, this is intended to complement it rather than compete with it.

    Your ASP code is interpreted by the server (hence "server side") while the page loads. Your Javascript is interpreted by the browser (hence "client side") after it has loaded. This means that you can't call ASP code directly with js.

    Use a conditional statement to check for a form submission. In place of function deleteclient use

    <%  
    If Request.Form("btnSubmit") <>"" Then
    ...
    ...
    End If
    %>
    

    Your form doesn't need an action attribute, as you aren't posting to a different page

    NB - it's considered good practice to use lower case tag names and to quote your attributes - eg

    <form name="addclient" method="post">