Domanda

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>
È stato utile?

Soluzione

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">

Altri suggerimenti

Don't do it in confirmation function itself. After confirmation just submit a form having your ASP page as action and some hidden field with a specific value.

On server side, just check Request.Form for that value and if it is passed - execute the function.

Of course solid server-side validation for things like deletion is a must.

P.S. it should be onclick="return confirmdelete()" and you confirmation should simple return true or false.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top