title: jQuery - Multiple form submission trigger unrelated multiple error messages
body: <p>I have couple of forms on my php page. I am using jQuery (&amp; Ajax) to process this page and show the results. Let's call the forms as: ToDo and Register forms. I have submit events attached to both the forms so that their corresponding processing takes place on submission. The ToDo form is located towards the top of the page and the Register form is located towards the bottom of the page. There are corresponding  tags right above these forms that show the error/success messages when the forms are processed. div #result_todo shows the errors obtained when ToDo form is submitted. div #result shows the errors obtained when Register form is submitted.</p>
  
  <p><strong>The actual problem:</strong>
  Let's say that I tried to submit ToDo form without filling any information. As expected, my related php file processes the submitted info and shows the errors in div #result_todo .
  There's a cross image located in this div which,when clicked upon, fades away the complete div #result_todo (&amp; it's errors as well)from user display when clicked upon. Now when I scroll to my Register form and try to submit it without filling any info, as expected, my related php file processes the submitted info and shows the errors in div #result. The problem now is that even the div #result_todo shows up along with the errors in it, when in fact it should not. This is unwanted as I wanted to submit only the Register form and I have to be shown only the errors related to this form and not the ToDo form.</p>
  
  <p>To reduce the amount of coding, I am calling the function to show the error messages in the success event(?) of the form submission. So I feel maybe that's where something is going wrong. I am very new to jQuery and javascripting, so kindly bear with me. The code is below:</p>
  
  <pre><code>&lt;script src="js/jquery-1.3.2.min.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
   function removeElement(divRemove) {
          $(divRemove).fadeOut(1000);
      }
  
  function theResult(data, popuDivDel)    {
  
      var popuDiv = popuDivDel;
  
      $(popuDiv).removeClass('success'); //remove the classes to avoid overlapping
      $(popuDiv).removeClass('error'); //remove the classes to avoid overlapping
  
      //If var success received from the php file is 0, then that means there was an error
      if(data.success == 0) 
          {                       
              $(popuDiv).html(data.message); //attach the message to the div
              $(popuDiv).addClass('error');  //attach the error class to the result div to show the errors
              //Add a link to dismiss the errors
              $(popuDiv).prepend('&lt;a href=\"javascript:;\" onclick=\"removeElement(\''+popuDiv+'\')\"&gt;&lt;img src="images/dismiss.png" alt="Dismiss Errors" title="Dismiss Errors" width="20" height="20" align="right" /&gt;&lt;/a&gt;');
          }                   
      else if(data.success == 1)  //means that our submission was good
          {
              $(popuDiv).html(data.message); //attach the message to the div
              $(popuDiv).addClass('success'); //attach the success class to the result div to show success msg
              setTimeout(function(){ $(popuDiv).fadeOut('slow'); }, 2000); //attach the animated effect as well
          }
  
  }
  &lt;/script&gt;
  
  
  
  &lt;script type="text/javascript"&gt;
  $(document).ready(function() {
  
      $().ajaxStart(function() {
          $('#loading').show();
          $('#result').hide();
  
          $('#loading_todo').show();
          $('#result_todo').hide();
      }).ajaxStop(function() {
          $('#loading').hide();
          $('#result').fadeIn('slow');
  
          $('#loading_todo').hide();
          $('#result_todo').fadeIn('slow');       
      });
  
  
      $('#myForm').submit(function() {
          $.ajax({
              type: 'POST',
              url: $(this).attr('action'),
              data: $(this).serialize(),
              dataType: 'json',
              success: function(data) //trigger this on successful reception of the vars from the php file
                 {
                     var popuDiv2 = '#result';
  
                     theResult(data, popuDiv2);
                 }
  
          })
          return false;
      });
  
  
  
      $('#frm_todo').submit(function() {
          $.ajax({
              type: 'POST',
              url: $(this).attr('action'),
              data: $(this).serialize(),
              dataType: 'json',           
              success: function(data) //trigger this on successful reception of the vars from the php file
                 {
                     var popuDivDel = '#result_todo';
  
                     theResult(data, popuDivDel);
                 }
  
          })
          return false;
      });
  })
  &lt;/script&gt;
  
  
  &lt;h4&gt;ToDo Form&lt;/h4&gt;
  &lt;div id="loading_todo" style="display:none;"&gt;&lt;img src="images/loading.gif" alt="loading..." /&gt;&lt;/div&gt;
  &lt;div id="result_todo" style="display:none;"&gt;&lt;/div&gt;
  
  &lt;form id="frm_todo" name="frm_todo" method="post" action="proses2.php"&gt;
   &lt;label for="todo"&gt;Enter to-do item:&lt;/label&gt;
  &lt;input type="text" name="todo" id="todo" /&gt;
  &lt;input type="submit" name="sbt_todo" id="sbt_todo" value="Add Item" /&gt;
  &lt;/form&gt;
  
  
  
  &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;
  
  
  
  
  &lt;h4&gt;REGISTER Form&lt;/h4&gt;
  &lt;div id="loading" style="display:none;"&gt;&lt;img src="images/loading.gif" alt="loading..." /&gt;&lt;/div&gt;
  &lt;div id="result" style="display:none;"&gt;&lt;/div&gt;
  &lt;form id="myForm" method="post" action="proses.php"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td width="100"&gt;Name&lt;/td&gt;
              &lt;td&gt;
                  &lt;input name="name" size="30" type="text" /&gt;&lt;?php echo (isset($error)) ? $error['name'] : ''; ?&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;e-mail&lt;/td&gt;
              &lt;td&gt;
                  &lt;input name="email" size="40" type="text" /&gt;&lt;?php echo (isset($error)) ? $error['email'] : ''; ?&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;phone&lt;/td&gt;
              &lt;td&gt;
                  &lt;input name="phone" size="40" type="text" /&gt;&lt;?php echo (isset($error)) ? $error['phone'] : ''; ?&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td&gt;comment&lt;/td&gt;
            &lt;td&gt;&lt;input name="comment" size="40" type="text" id="comment" /&gt;&lt;?php echo (isset($error)) ? $error['comment'] : ''; ?&gt;&lt;/td&gt;
        &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;/td&gt;
              &lt;td&gt;
                  &lt;input name="submit" type="submit" id="submit" value="Submit" /&gt;
                  &lt;input type="reset" value="Reset" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form&gt;
  
  
  
  &lt;?php //proses.php
  //validation
  
  if (trim($_POST['name']) == '') {
      $error['name'] = '- Name Must Fill';
  }
  if (trim($_POST['email']) == '') {
      $error['email'] = '- Email Must Fill &amp; Valid Mail';
  }
  if (trim($_POST['phone']) == '') {
      $error['phone'] = '- Phone must be filled';
  }
  if (trim($_POST['comment']) == '') {
      $error['comment'] = '- Comment must be filled';
  }
  
  if ($error) 
      {
          $success = 0;
          $message = '&lt;b&gt;Error&lt;/b&gt;: &lt;br /&gt;'.implode('&lt;br /&gt;', $error);
      } 
  else 
      {
          $success = 1;
          $message = '&lt;b&gt;Thank You For Filling This Form&lt;/b&gt;&lt;br /&gt;';  
      }
  
  
  print json_encode(array('success' =&gt; $success, 'message' =&gt; $message)); 
  die();
  ?&gt;
  
  &lt;?php //proses2.php
  //validation
      if (trim($_POST['content']) == '') 
      {
          $error['content'] = '- Must Fill Todo';
      }
  
  
      if ($error) 
      {
          $success = 0;
          $message = '&lt;b&gt;Error&lt;/b&gt;: &lt;br /&gt;'.implode('&lt;br /&gt;', $error);
          $message .= $error['content'];
      } 
      else 
      {
          $success = 1;
          $message = '&lt;b&gt;Thank You For Filling This Form&lt;/b&gt;&lt;br /&gt;';  
      }
  
  
  
  print json_encode(array('success' =&gt; $success, 'message' =&gt; $message)); 
  die();
  ?&gt;
  </code></pre>
  
  <p>Please suggest me a possible solution to this problem while making an efficient use of the functions to reduce the amount of coding it might require to achieve common objectives (like showing the results with their corresponding effects).</p>
  
  <p>Thank you in advance.</p>