• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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