Solved

confirm alert error

Posted on 2010-09-22
11
429 Views
Last Modified: 2012-05-10
hello
i am trying to use a confirm alert so should the user cancel then no further action will be called. however, it is still calling the ajax call even if i click cancel. it is appearing as line 4 in the attached code. can someone show me the error? many thanks
function edit(com, grid) {
			if (com == 'Edit') {
        	if($('.trSelected').length>0){
            		confirm("Are you sure you wish to edit this record?");
            }
            var items = $('.trSelected');
            var itemlist ='';
            for(i=0;i<items.length;i++){
                itemlist+= items[i].id.substr(3);
            }
         $.ajax({
            type: "POST",
            dataType: "json",
            url: "boxedit.php",
            data: "items="+itemlist,
            success: function(data){
            id = data.id;
			custref = data.custref;
			status = data.status;
			//confirm("Are you sure you wish to edit: "+data.id+"\n"+data.custref);
			$.facybox({ div: '#fileeditform' }); 
			$("#facybox").find("#id").attr({value: id });
            $("#facybox").find("#custref").attr({value: custref });
			$("#facybox").find("#boxref").attr({value: status });
			$("#flex1").flexReload();
            }
			
          });
           
            /*and so on then you can call facybox*/
          //$("#flex1").flexReload();
         
        } else{
            alert('Please select a row to edit.');
        }
    }

Open in new window

0
Comment
Question by:peter_coop
11 Comments
 
LVL 4

Expert Comment

by:rstjean
ID: 33734878
if(!confirm("Are you sure you wish to edit this record?")){return false;}
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33734883
replace line number 4 by

if ( !confirm("Are you sure you wish to edit this record?") )
{
  return false;
}

check for the similar cases
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 33735157
another one :


<script language="javascript">
	function edit(com, grid) {
		if(com == 'Edit') {
			if( $('.trSelected').length>0 ) {
				if( confirm("Are you sure you wish to edit this record?") ) {
					var items = $('.trSelected');
					var itemlist ='';
					for(i=0;i<items.length;i++){
						itemlist+= items[i].id.substr(3);
					}
					$.ajax({
						type: "POST",
						dataType: "json",
						url: "boxedit.php",
						data: "items="+itemlist,
						success: function(data){
							id = data.id;
							custref = data.custref;
							status = data.status;
							//confirm("Are you sure you wish to edit: "+data.id+"\n"+data.custref);
							$.facybox({ div: '#fileeditform' }); 
							$("#facybox").find("#id").attr({value: id });
							$("#facybox").find("#custref").attr({value: custref });
							$("#facybox").find("#boxref").attr({value: status });
							$("#flex1").flexReload();
						}
					});
					/*and so on then you can call facybox*/
					//$("#flex1").flexReload();			
				}
			}
			else {
				alert('Please select a row to edit.');
			}
		}
	}
</script>

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33735874
Simpler

original:

if (com == 'Edit') {               if($('.trSelected').length>0){                         confirm("Are you sure you wish to edit this record?");             }


if (com == 'Edit' ) {               if($('.trSelected').length>0 &&                         confirm("Are you sure you wish to edit this record?"); {
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33735917
@mplungjan : check line 33

we've :

if($('.trSelected').length>0){
   inssert row
}
else {
   alert('Please select a row to edit.');
}

length>0 of but the user answer no so we get the alert : Please select a row to edit.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33737530
Ah, not the way his brackets were from the start
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33737638
check the number of bracket at the end
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33737656
At the beginning:

if (com == 'Edit') {
  if($('.trSelected').length>0){
    confirm("Are you sure you wish to edit this record?");
   }
.
.
.
}
else
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33737684
At the end :

                  else {
                        alert('Please select a row to edit.');
                  } // "else" of if($('.trSelected').length>0){
            } // if (com == 'Edit') {
      } // function bracket
0
 

Author Closing Comment

by:peter_coop
ID: 33739415
thank you very much for the help.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33739603
You're welcome! Thanks for the points!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web forms being filled out by bots 4 36
Html5 Index on a table 7 27
CSS: Making Pure CSS read more boxes thinner 5 31
where is team.js? 3 27
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question