Solved

confirm alert error

Posted on 2010-09-22
11
425 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now