views:

113

answers:

2

Hi,

I have a pop up form that i wish to close on successfull submit of the form. Currently we are only validating on the server side which will create an unordered list of validation errors with the class .error_message.

Once the form has been successfully submitted i want to close the window.

Currently the jQuery i am using for this is as follows but does not work because the first time the form has been submitted the number of error messages will always be zero.

$(document).ready(function() {

$('form#emailQuote').submit(function() {

    var $emailErrors = $('form#emailQuote ul.error_message').length;

    alert($emailErrors);

    if ($emailErrors == 0) {
        //window.close();
        alert('no errors');

    } else {
        //alert('errors');
        alert('errors');
    }      
});

});

+1  A: 

You can do something like this (here server side uses php, but can be anything) :

<form onsubmit="javascript:do_submit(); return false;"  method="post" >
.... 
</form>

<script type="text/javascript">
function do_submit()
{
  var params = {"param1":$("elem1").val(), "param2":$("elem2").val(), ....};
  $.ajax
  ({
      url:"ajax_handler.php",
      type:"POST",                      
      data:params,
      success: function(retval)
      {                             
         var err_code;
         if (! $.browser.msie)
         {
           if ($('error_code:first',retval))
           {
             err_code = $('error_code:first',retval).text();   
             //check err_code and show error, or redirect to a different page ,or close window             
           }
           else 
           {
               //bad xml, show error message
           }
         }
         else // we need to load xml differently in case of IE
         {
            var xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            try
            {
               xml.loadXML(retval);
               err_code = xml.documentElement.getElementsByTagName('error_code')[0].text;
               //check err_code and show error, or redirect to a different page ,or close window
             }
             catch (error)
             {
               //bad xml, show error message
             }
         }
      }
    });
}
</scirpt>

Php:
<?php
  $xml = new DOMDocument( "1.0", "UTF-8" );
  $xml_root = $xml->createElement("response");        
  $xml->appendChild($xml_root);    
  $xml_code  = $xml->createElement("error_code");
  $xml_root->appendChild($xml_code);
  // process request 
  // ....
  $xml_val = $xml->createTextNode(utf8_encode(0)); // 
  $xml_code->appendChild($xml_val);

  print($xml->saveXML());
?>
a1ex07
I think we are going to set a flag in session and output a hidden form variable when everything has been successful and then close the window then.
RyanP13
A: 

there is anyway to click the close button of the closing popup to redirect into another page ?

Phuc Nguyen