Solved

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

Posted on 2013-05-29
5
369 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now