Display prompt before continuing

sirbounty
sirbounty used Ask the Experts™
on
I have a button to delete checked items in a list.
I want to display a confirmation prompt before continuing with the delete routine.
I'm not certain how to do this.
I've tried with some code I found online that has a javascript function in the aspx code, but it doesn't seem to process that script.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Can you post the code you tried, or a link to it?
Hi,

Here the way I do it

function deleteThis(passing value here){
	
	var c = confirm(are_you_sure);
	
	if(c) {
			
		$.ajax({
			type: 'POST',
			your code here....
		},
				
	});
   }	
}

Open in new window

Top Expert 2007

Author

Commented:
lenamtl - web dev newbie here - where would I place that code?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2007

Author

Commented:
This is what I'd found, that doesn't seem to fire - it's in my head tag:

     <script type = "text/javascript">
         function DeleteList() {
             var confirm_value = document.createElement("INPUT");
             confirm_value.type = "hidden";
             confirm_value.name = "confirm_value";
             if (confirm("Are you sure you want to delete all selected lists?")) {
                 confirm_value.value = "Yes";
             } else {
                 confirm_value.value = "No";
             }
             document.forms[0].appendChild(confirm_value);
         }
    </script>
Hi,

What are you deleting exactly?

Usually on confirm alert you have OK and Cancel button
Confirm example
https://www.w3schools.com/jsref/met_win_confirm.asp

Prompt example
https://www.w3schools.com/jsref/met_win_prompt.asp

If you passing form values to ajax then use what I provide earlier.

function deleteThis(passing value here){
	
	var c = confirm(are_you_sure);
	
	if(c) {
			
		$.ajax({
			type: 'POST',
			your code here....
		},
				
	});
   }	
}

Open in new window


If you are not using Ajax and deleting an element or hiding an element
then you can reuse a part of this

var c = confirm(are_you_sure);
	
if(c) { // do something

else { //do nothing

Open in new window


You won't need to assign a yes or no value just pass the code you want to happen if OK is pressed

I have to go now I will be back later
leakim971Multitechnician
Top Expert 2014

Commented:
test page : https://jsfiddle.net/k8ob4ehL/

<form onsubmit="return askIfSure()">
  <ul>
    <li><input type="checkbox" id="cb1" name="cb1" /><label for="cb1">some1</label></li>
    <li><input type="checkbox" id="cb2" name="cb2" /><label for="cb2">some2</label></li>
    <li><input type="checkbox" id="cb3" name="cb3" /><label for="cb3">some3</label></li>
  </ul>
  <input type="submit" value="delete list">
</form>

Open in new window


function askIfSure() {
	var boolAnswer = confirm("are you sure to delete list of item checked?");
  return boolAnswer; // true or false. if false the form is not submitted
}

Open in new window

Top Expert 2007

Author

Commented:
Hi lenamtl - I'm not actually deleting anything from the code.
There will be 3 buttons - create/modify/delete.
The delete will accept one or more selected items from the listbox.
I want to display the item(s) selected, then prompt with "Are you sure?".
If they click no/cancel, nothing happens.
If they click yes - it will write a file to be placed in a queue for system processing.

leakim971 - if I change the form to respond that way, does it require further logic for other buttons?
Hi,

You need a form /and or a Javascript action (ie onclick) to send the values, then Javascript will display the are you sure message the confirm() will do what you want,
then if yes the data are send (I'm using Ajax) to the server side code let say PHP to do some validations and if this is ok proceed the request if this is not ok display a message to the user (your cannot delete this)...

You may not need a form for example if you have a table and a delete row button you can just pass the values using onclick for that button ID then performed the logical above..
leakim971Multitechnician
Top Expert 2014

Commented:
leakim971 - if I change the form to respond that way, does it require further logic for other buttons?

it look like no. did you check the test page I submited ? the submit button did not change
Developer User Interface
Commented:
considering that you are using an asp.net button control for the delete button you would use

function DeleteList()

as below:
<script type = "text/javascript">
         function DeleteList() {
             if (confirm("Are you sure you want to delete all selected lists?")) {
                 return true;
             } else {
                 return false;
             }
         }
    </script>

and in your body tag
<asp:Button id="btnDelete" runat="server" OnClientClick="return DeleteList()" />
Top Expert 2007

Author

Commented:
I've attempted a few different ways, but I'm afraid it's been too long since I've worked with web development.
Obadiah - where would I put this script?  How could I call another function if confirmed?
Top Expert 2007

Author

Commented:
I think I'm going to close this and start with the basics.  I don't want to keep chasing my tail on something I might have to change anyway...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial