• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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

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
sabecs
Asked:
sabecs
  • 3
  • 2
1 Solution
 
TvMptCommented:
$('#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
 
sabecsAuthor Commented:
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
 
TvMptCommented:
if (confirm("Are you sure you want to lock this form?"))
{
   $(this).closest('form').submit();
}
0
 
sabecsAuthor Commented:
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
 
TvMptCommented:
You have the  e.value to make the logic
compare if is ticked or not and if not just return and do nothing
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now