PDA

View Full Version : Javascript In ASP Nightmare



Ayomide
06-06-2005, 03:40 PM
I have an .asp page that I need to have a confirm message box returns false and not delete the record then goes back to the previous page or true as needed, delete the record and continues with the current page. My code, listed below, shows the correct popup message but continues to delete the record.

Help Now Please!!!

Ayomide.

If NOT objRS.EOF Then
msg="Employee ID Number: " +objRS("EmpLocatorID")+ " exists for " +objRS("EmpLastName")+ ", " +objRS("EmpFirstName")
Response.Write "<" & "script language=javascript>"
Response.Write "var msg = '"+msg+"';"
Response.Write "var x=confirm(msg + '\nAre you sure want to delete this employee record?');"
Response.Write "if(x==false)"
Response.Write "{"
Response.Write "window.history.go(-1);"
Response.Write "}"
Response.Write "<" & "/script>"
objComm.CommandText = "DELETE from Listing WHERE EmpId = '"&strEmpID&"';"
objComm.CommandType = adCmdText
objComm.Execute intNoOfRecords
End If

mwinter
06-06-2005, 07:01 PM
I have an .asp page that I need to have a confirm message box returns false and not delete the record then goes back to the previous page or true as needed, delete the record and continues with the current page.Using Javascript for such a critical operation is not a good idea, except perhaps in a controlled environment (such as a private intranet) where you can guarantee that all users will have the necessary support. It would be better to use a separate confirmation stage.


If NOT objRS.EOF Then
msg="Employee ID Number: " +objRS("EmpLocatorID")+ " exists for " +objRS("EmpLastName")+ ", " +objRS("EmpFirstName")

[To-client output]

objComm.CommandText = "DELETE from Listing WHERE EmpId = '"&strEmpID&"';"
objComm.CommandType = adCmdText
objComm.Execute intNoOfRecords
End IfAs I'm reading this, it seems that you're confusing how server-side processing interacts with the client; you will have written the confirmation code, but deletion occurs immediately after the output has been written. The query will have been executed before the network will have relayed the response to the client. The confirmation either has to appear when the client initially requests deletion (that is, in the document that contains the delete command), or in a separate document that is shown between the deletion request and the document that performs that action.


Response.Write "<" & "script language=javascript>"Might I ask: why are you performing a separate concatenation? It makes no difference client-side, and I doubt it does server-side either (but I could be wrong on the latter). In addition, the language attribute is deprecated - you should be using the (required) type attribute instead:


<script type="text/javascript">Mike

Ayomide
06-06-2005, 08:25 PM
Mike,
Am I understanding you correctly, the javascript, which is client-side, needs to be place on the page that is calling the SQL delete code? If this is valid, then I have a greater challegne in calling two functions on the sending page that using the forms tag.

I place my existing code into the asp page as a separate function calling it from within the form tag and it is not being recognized.

Addititionally, I would like to reflect within the confirm message box the value the user entered in the specific text box.

These web pages are being hosted on an Intranet.

How do I get this accoplished?

mwinter
06-06-2005, 09:09 PM
Am I understanding you correctly, the javascript, which is client-side, needs to be place on the page that is calling the SQL delete code?It would need to placed on the form or button that is used to initiate the deletion. For example:


function confirmDeletion() {
return confirm('Are you sure want to delete this employee record?');
}

<input type="submit" name="operation" value="Delete"
onclick="return confirmDeletion();">


If this is valid, then I have a greater challegne in calling two functions on the sending page that using the forms tag.If you mean you have a function that is already called during submission (say, a validation function) then this fairly trivial to accomodate.

If the existing function is just 'some function' that doesn't affect whether the form is sent, then just place a semicolon after the first function call:


<form action="..." method="..."
onsubmit="originalFunction(); return confirmDeletion();">If the existing function is a validation function, or something similar, then you can combine the return value with the confirmation function using the logical AND (&&) operator:


<form action="..." method="..."
onsubmit="return originalFunction() && confirmDeletion();">


Addititionally, I would like to reflect within the confirm message box the value the user entered in the specific text box.That too is quite simple. Assuming the text box is named employeeID, then you could change the above to:


function confirmDeletion(message) {
return confirm(message
+ '\nAre you sure want to delete this employee record?');
}

<form action="..." method="..."
onsubmit="return originalFunction()
&& confirmDeletion('Employee ID: ' + this.elements.employeeID.value);">

Mike

Ayomide
06-07-2005, 02:19 PM
Mike,
Thank you very much for the help you gave me.
I was able to get the last part of my web page completed due totally to the directions you rendered to me. :D

Keep Teaching.

Ayomide