1

Using impromptu in jquery to submit a form when a checkbox is clicked. If the user submits the form, everything works fine. However, if the user cancels the form, the checkbox stays clicked. I would like to tie an action to the cancel event to close the prompt AND uncheck all checkboxes with a certain ID. How can I close the form AND uncheck all checkboxes if a user cancels. Here's what I have so far -- of special interest is the last line that actually processes the form, but I'm including the whole function in case it helps:

    function markComplete(activity_id,index)
 {
    var addform = 
        '<h3>Form title</h3>\n\
         <form\n\
           <!--Form stuff goes here --> \n\
        </form>';

    function validate(v,m,f) //prevents submission on empty notes field
  {
  var flag = true;
  if(v == 1)
          {
            if ($("#notes").val()==''){
              $("#notes").addClass("form-error-input");
              flag = false;
            }
            else $("#notes").removeClass("form-error-input");
          }
          return flag;
}

  function callback_add(v,m,f){
      if(v == 1)
      {
        var dataString = 'completion_date='+ f.completion_date + "&notes=" + f.notes;
        $.post("<?php echo $_SERVER['REQUEST_URI']; ?>?mark_complete",dataString);
        $("tr").eq(index).hide("fast");
        $.prompt.close();
      }
      else
      {
        $("#complete_check").removeAttr('checked');
        $.prompt.close();
      }
    }
    $.prompt(addform,{ submit: validate, callback: callback_add, buttons: { Cancel: function (){$("#complete_check").removeAttr('checked');}, Add: 1 },focus: 1 });

This function is called when a checkbox clicked, shown here:

<input name="mark_complete" id="complete_check" type="checkbox" onchange="markComplete(<?php echo $record['activity_id']; ?>,$(this).index())" />
user882134
  • 279
  • 3
  • 16

1 Answers1

0

I had to modify the validate code to get this to work:

From:

function callback_add(v,m,f){
      if(v == 1)
      {
        var dataString = 'completion_date='+ f.completion_date + "&notes=" + f.notes;
        $.post("<?php echo $_SERVER['REQUEST_URI']; ?>?mark_complete",dataString);
        $("tr").eq(index).hide("fast");
        $.prompt.close();
      }
      else
      {
        $("#complete_check").removeAttr('checked');
        $.prompt.close();
      }

To:

function callback_add(v,m,f){
      if(v == 1)
      {
        var dataString = 'completion_date='+ f.completion_date + "&notes=" + f.notes;
        $.post("<?php echo $_SERVER['REQUEST_URI']; ?>?mark_complete",dataString);
        $("tr").eq(index).hide("fast");
        $.prompt.close();
        $("#new_row").addClass("form-success");
      }
      else
      {
        $('input[type=checkbox]:not("#todo_check")').each(function() //all checkboxes except the "To Do", which should remain checked
      { 
        this.checked = false; 
      });
     }

Worked like a champ!

user882134
  • 279
  • 3
  • 16