title: Php Ajax form submit in colorbox
body: <p>I have a form  with some php to validate and insert in the database on submit and the form opens in colorbox.</p>
  
  <p>So far so good. What I'm trying to do is to close colorbox and refresh a div on success.</p>
  
  <p>I guess I need to pass a response to ajax from php if everything OK, close the colorbox with something like setTimeout($.fn.colorbox.close,1000); and refresh the div, but I'm stuck because I'm new in ajax.</p>
  
  <p>I'll appreciate any help here.</p>
  
  <p>Here is my ajax:</p>
  
  <pre><code>jQuery(function(){
   jQuery('.cbox-form').colorbox({maxWidth: '75%', onComplete: function(){
     cbox_submit();
  
    }});
  });
  
  
  function cbox_submit()
  {
    jQuery("#pre-process").submit(function(){
     jQuery.post(
        jQuery(this).attr('action'),
        jQuery(this).serialize(),
        function(data){
         jQuery().colorbox({html: data, onComplete: function(){
            cbox_submit();
  
          }});
        }
      );
      return false;
    });
  }
  </code></pre>
  
  <p>form php code:</p>
  
  <pre><code>    &lt;?php
  error_reporting(-1); 
  include "conf/config.php";
  
  if(isset($_REQUEST['rid'])){$rid=safe($_REQUEST['rid']);}
  if(isset($_REQUEST['pid'])){$pid=safe($_REQUEST['pid']);}
  $msg = '';
  if (!$_SESSION['rest_id']) $_SESSION['rest_id']=$rid; //change to redirect
  
  $session_id=session_id();
  
  
  if(isset($_REQUEST['submit'])){
  
  if(isset($_POST['opta'])){
  $opta=safe($_POST['opta']);
  $extraso = implode(',',array_values( array_filter($_POST['opta']) ));
                      }
  
  if (array_search("", $_POST['opt']) !== false) 
  {
  $msg = "Please select all accessories!";
  }else{
  
  $extrasm = implode(',',array_values( array_filter($_POST['opt']) ));
  
  if ($_POST['opt'] &amp;&amp; isset($_POST['opta'])) {$extras= $extrasm .",". $extraso;}
  if ($_POST['opt'] &amp;&amp; !isset($_POST['opta'])) {$extras= $extrasm;}
  if (!$_POST['opt'] &amp;&amp; isset($_POST['opta'])) {$extras= $extraso;}
  
  $sql['session_id']  = $session_id;
      $sql['rest_id'] = $_POST['rid'];
      $sql['prod_id'] = $_POST['pid'];
      $sql['extras']  = $extras;
      $sql['added_date']  = Date("Y-m-d H:i:s");
      $newId=insert_sql("cart",$sql);
  
      }
  }
  ?&gt;
  
  &lt;form id="pre-process" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post"&gt;
  &lt;div style="background-color:#FFF; padding:20px;"&gt;
  &lt;?=$msg;?&gt;
    &lt;?php
    $name = getSqlField("SELECT name FROM products WHERE resid=".$_SESSION['rest_id']." and id=".$pid."","name");
    echo "&lt;div style='color:#fff; background-color:#F00;padding:10px;' align='center'&gt;&lt;h2&gt;".$name."&lt;/h2&gt;&lt;/div&gt;&lt;div style='background-color:#FFF; padding: 20px 70px 30px 70px; '&gt;Please select accessories.&lt;br&gt;&lt;br&gt;";
   $getRss = mysql_query("SELECT * FROM optional_groups_product where prodid=".$pid." order by id asc");
      while ($rsrw = @mysql_fetch_array($getRss)) {
  
    $goptionals = getSqlField("SELECT goptionals FROM optionals_groups WHERE resid=".$_SESSION['rest_id']." and id=".$rsrw['goptid']."","goptionals");
  
          $goptionals=explode(', ',($goptionals));
  
  echo "&lt;select name='opt[]' id='opt[]' style='width:220px;'&gt;";
  echo "&lt;option value='' &gt;Select Options&lt;/option&gt;";
      foreach($goptionals as $v)
      {
           $vname = mysql_query("SELECT * FROM optionals where id=".$v." LIMIT 0,1");
           while ($rsgb = @mysql_fetch_array($vname)) {
               $aa=$rsgb['optional'];
           }
               echo "&lt;option value=".$v." &gt;".$aa."&lt;/option&gt;";
  
  
      }
           echo "&lt;/select&gt;(required)&lt;br&gt;";       
      //}
          }
          $getRss = mysql_query("SELECT * FROM optional_product where prodid=".$pid.""); 
          ?&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;table border="0" cellpadding="0" cellspacing="0" &gt;
         &lt;tr&gt;
      &lt;td bgcolor="#EAFFEC"&gt;
  &lt;div style="width:440px; "&gt; 
  &lt;?php
  
  while ($rssp = @mysql_fetch_array($getRss)) {
   $optional=getSqlField("SELECT optional FROM optionals WHERE id=".$rssp['optid']."","optional");
   $price=getSqlField("SELECT price FROM optionals WHERE id=".$rssp['optid']."","price");
  ?&gt;
  &lt;div style="width:180px;background-color:#EAFFEC; float:left;padding:10px;""&gt;&lt;input type="checkbox" name="opta[]" id="opta[]" value="&lt;?=$rssp['optid']?&gt;"  /&gt; &lt;i&gt;&lt;?=$optional?&gt; [&lt;?=CURRENCY?&gt;&lt;?=$price?&gt; ]&lt;/i&gt; &lt;/div&gt;
  &lt;?php } ?&gt;
  &lt;/div&gt;
  
  &lt;/td&gt;
   &lt;/tr&gt;&lt;/table&gt;
  &lt;input type="hidden" name="rid" value="&lt;?=$rid?&gt;" /&gt;
  &lt;input type="hidden" name="pid" value="&lt;?=$pid?&gt;"/&gt;
      &lt;/div&gt;&lt;input type="hidden" name="submit" /&gt;&lt;input  id='submit' class="CSSButton"    style="width:120px; float:right;" name='submit' type='submit' value=' Continue ' /&gt;&lt;br /&gt;
  
  &lt;br /&gt;&lt;br /&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>