JS Close window on form submission
<p>Update at bottom of post:</p>

<p>I have a page where when a button is pressed, a new window will popup with a form.  Below is where I open the popup.</p>

<pre><code>&lt;a&gt;&lt;span class="add-on"&gt;&lt;i class="icon-plus" style="color: green;" onclick="window.open('&lt;?php echo $this-&gt;Html-&gt;Url(array('controller' =&gt; 'customers', 'action' =&gt; 'add?popup')); ?&gt;', 'Add Customer', 'height=630, width=430')"&gt;&lt;/i&gt;&lt;/span&gt;&lt;/a&gt;
</code></pre>

<p>When the form is submitted, I need to close that window.  The problem I am having now is that the window is closing before the form is fully getting submitted.</p>

<p>Popup Window Code:</p>

<pre><code>&lt;fieldset style="padding-left: 15px;"&gt;
&lt;legend&gt;Add Customer&lt;/legend&gt;

&lt;?php echo $this-&gt;Form-&gt;create('Customer', array('inputDefaults' =&gt; array('div' =&gt; false, 'label' =&gt; false))); ?&gt;
    &lt;div class="control-group"&gt;
        &lt;div class="input-prepend"&gt;
            &lt;span class="add-on"&gt;&lt;i class="icon-user"&gt;&lt;/i&gt;&lt;/span&gt;
            &lt;?php echo $this-&gt;Form-&gt;input('first_name', array('placeholder' =&gt; 'First Name')); ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="control-group"&gt;
        &lt;div class="input-prepend"&gt;
            &lt;span class="add-on"&gt;&lt;i class="icon-user"&gt;&lt;/i&gt;&lt;/span&gt;
            &lt;?php echo $this-&gt;Form-&gt;input('last_name', array('placeholder' =&gt; 'Last Name')); ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="input-prepend"&gt;
        &lt;span class="add-on"&gt;&lt;i class="icon-globe"&gt;&lt;/i&gt;&lt;/span&gt;
        &lt;?php echo $this-&gt;Form-&gt;input('location', array('placeholder' =&gt; 'Location')); ?&gt;
    &lt;/div&gt;

    &lt;div class="control-group"&gt;
        &lt;div class="input-prepend"&gt;
            &lt;span class="add-on"&gt;&lt;i class="icon-phone"&gt;&lt;/i&gt;&lt;/span&gt;
            &lt;?php echo $this-&gt;Form-&gt;input('phone_number', array('class' =&gt; 'maskedPhone', 'placeholder' =&gt; 'Phone Number')); ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="control-group"&gt;
        &lt;div class="input-prepend"&gt;
            &lt;span class="add-on"&gt;&lt;i class="icon-envelope"&gt;&lt;/i&gt;&lt;/span&gt;
            &lt;?php echo $this-&gt;Form-&gt;input('email', array('placeholder' =&gt; 'Email')); ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;!-- Close Form --&gt;
&lt;?php echo $this-&gt;Form-&gt;end(array(
    'label' =&gt; 'Save Customer',
    'id' =&gt; 'saveCust',
    'class' =&gt; 'btn btn-primary',
    'div' =&gt; false
)); ?&gt;
</code></pre>

<p></p>

<pre><code>&lt;?php echo $this-&gt;Html-&gt;script('jquery.maskedinput.min', array('inline' =&gt; false));
  echo $this-&gt;Html-&gt;script('maskedinput', array('inline' =&gt; false));

$this-&gt;start('script'); ?&gt;
    &lt;script type="text/javascript"&gt;
        $(document).ready(function () {     
            inputs.maskedInputs();

            // Get queryString to close popup window
            var queryString = window.location.search.substring(1);
            if (queryString == "popup"){
                $('#CustomerAddForm').submit(function() {
                    window.close();                 
                });                 
            }
        });
    &lt;/script&gt;
&lt;?php $this-&gt;end(); ?&gt;
</code></pre>

<hr>

<p>Trying to use Ajax call now with no luck:</p>

<pre><code>$('#saveCust').click(function() {
                    alert("test");
                    $.ajax({
                        type: "POST",
                        url: "&lt;?php echo $this-&gt;Html-&gt;Url(array('controller' =&gt; 'Customers', 'action' =&gt; 'add')); ?&gt;",
                        data: $('#CustomerAddForm').serialize(),
                        success: function(data)
                        {
                            alert("Test");
                            window.close();
                        }
                    })
                });
                return false;               
</code></pre>
