Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript IF THEN statement not working

Status
Not open for further replies.

cruford

Programmer
Dec 6, 2002
138
US
I have a delete confirmation javascript that is supposed to cancel the delete if the user chooses. It is deleting the record no matter what choice the user picks.

Code:
<script language="JavaScript">
   var intAnswer;
function DeleteAccount(ID,FIRST,LAST) {
		intAnswer = window.confirm("Delete Account: " + FIRST + " " + LAST + " ?");
		if (intAnswer == 1) {
			window.location.replace("Diabetics_Main.asp?CMD=Delete&ID=" + ID);
		}
		return false;
  }
</script>

Here is the delete hyperlink the user is clicking to invoke this:

Code:
Response.Write "<td><a onClick=""DeleteAccount(" & RS.fields("ID") & ", '" & RS.Fields("FirstName") & "', '" & RS.Fields("LastName") & "')"" style=""text-decoration: none; color: navy; font-size: 8pt"" href=""Diabetics_Main.asp?CMD=Delete&ID=" & RS.Fields("ID") & "&" & uUrl & """>Delete</a></td>"

The confirmation comes up just fine but it doesn't matter if I click OK or CANCEL it deletes the record. I don't know javascript that well so if any can point out my mistake I would appreciate it.

Thanks
 
mm. The Way you have it the loop iterates atleast once so it will go thru to the window.replace once.

you should try
Code:
<script language="JavaScript">

function DeleteAccount(ID,FIRST,LAST) {
    if (confirm("Delete Account: " + FIRST + " " + LAST + " ?")) {
            window.location.replace("user_del.asp?ID=" + ID );

}
</script>

since that will provide the script with a cancel function.
 
the reason why it always iterates because you have it where if the confirm comes up it will produce a true. well comfirm always produces a true if you call it. so it will always go thru that windows replace.
 
I copied your code from the post above and changed my window.location.replace to point to the correct page and it still deletes the record no matter which button I select. I am not sure what I am missing or doing wrong now. Any other ideas?
 
hmm so using the code it still go through ? can you paste exactly what you used. Because supposedly it shouldn't if you click cancel.
 
ahh wait i see
you still have this in your code

Code:
href=""Diabetics_Main.asp?CMD=Delete&ID=" & RS.Fields("ID") & "&" & uUrl & """>
take that out. because it's directly linking to your delete script.
 
Ok taking the HREF out worked but it caused another issue. I was doing a response.redirect after the delete to request.servervariables("HTTP_REFERER"):

Code:
	Select Case Request.QueryString("CMD")
		Case "Delete"
			intLen = Len(request.querystring("uUrl"))
			intLen = intLen + 5
			sRef = Left(request.servervariables("HTTP_REFERER"),(Len(request.servervariables("HTTP_REFERER")) - intLen))
			sRef = sRef & "uUrl=" & Date & Timer
			sSQL = "DELETE FROM ztblDiabeticLog WHERE ID =" & request.querystring("ID")
			Set RS = Conn.Execute(sSQL,,1)
			response.redirect(sRef)

Now because I removed the HREF portion of my link the HTTP referer variable is not correct. Is there anyway to read what's currently in the address bar into a variable so I can redirect back to it?

Basically the user clicks the DELETE link and it confirms the delete. Then it should redirect back to the page they were just on. Now that page may have various querystring elements in it which is why I use the referer method, if I don't it spits them back to the default view of the page and they have to resort and filter the records again. Hope this makes since.

Thanks
 
why dont u pass a url string to the function as well. name it uUrl and in the case statement above remove the .querystring part so it will pick up anything with the uUrl variable in it. remember to pass it to the javascript by adding another argument .

Code:
<script language="JavaScript">

function DeleteAccount(ID,FIRST,LAST,uURL) {
    if (confirm("Delete Account: " + FIRST + " " + LAST + " ?")) {
            window.location.replace("user_del.asp?ID=" + ID + "uURL=" uURL );

}
</script>

so basically you can pass any url right into the function so your next page can accept it.
 
also to read what's in the script name you can use Request. ServerVariables (SCRIPT_NAME) to get it from your page and write it out as
DeleteAccount(ID,FIRST,LAST,<%=Request. ServerVariables (SCRIPT_NAME)%>)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top