Solved

Submit form from a checkbox with a confirm message using JavaScript/jQuery?

Posted on 2013-05-29
5
372 Views
Last Modified: 2013-06-03
Is there a way to submit a form from a checkbox with a confirm message using JavaScript/jQuery?
I basically want to display a message saying something like "Are you sure you want to lock this form?" prior to be submitted.

<form action="<?php echo $editFormAction; ?>" method="POST"  enctype="multipart/form-data" name="upd_application" id="upd_application">

<input name="row_view_appl[locked]" type="checkbox" id="locked"  value="1" >

Open in new window

0
Comment
Question by:sabecs
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39204020
$('#upd_application').click( function(e){
  $('#locked').val( e.value ); // capture on check or uncheck
  $(this).closest('form').submit();
});

Open in new window


Instead click you can use change too...
0
 

Author Comment

by:sabecs
ID: 39204053
Thanks  TvMpt, that's great but how can we add a confirm message saying something like "Are you sure you want to lock this form?" prior to be submitted.?
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39204064
if (confirm("Are you sure you want to lock this form?"))
{
   $(this).closest('form').submit();
}
0
 

Author Comment

by:sabecs
ID: 39204169
Thanks again, this is what I have so far but if they click cancel the box stays ticked?
If they un tick it the message pops up again?

is there an easy way for it to only come up if the tick the box and also un tick the box if they select cancel on the confirm message?


$('#locked').click( function(e){
       if (confirm("Are you sure you want to lock this form?")){
         $(this).closest('form').submit();
}
});



<input name="locked" type="checkbox" id="locked"  value="Y"/>
0
 
LVL 9

Accepted Solution

by:
TvMpt earned 500 total points
ID: 39204300
You have the  e.value to make the logic
compare if is ticked or not and if not just return and do nothing
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get the number of current month days 2 23
HTML5 frame 5 25
JavaScript behaviour different on local machine and network share 11 45
Index on a Table 6 8
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 …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

775 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