Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

Display prompt before continuing

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.
Avatar of Norie
Norie

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

Avatar of sirbounty

ASKER

lenamtl - web dev newbie here - where would I place that code?
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
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

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..
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
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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...