简体   繁体   English

PHP Ajax表单在色箱中提交

[英]Php Ajax form submit in colorbox

I have a form with some php to validate and insert in the database on submit and the form opens in colorbox. 我有一个带有一些php的表单,用于在提交时验证并插入数据库中,并且该表单在colorbox中打开。

So far so good. 到现在为止还挺好。 What I'm trying to do is to close colorbox and refresh a div on success. 我想做的是关闭颜色框,并在成功后刷新div。

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); 我想如果一切正常,我需要从php传递一个对ajax的响应,用setTimeout($。fn.colorbox.close,1000)之类的方法关闭颜色框; and refresh the div, but I'm stuck because I'm new in ajax. 并刷新div,但由于我是ajax的新手,所以我被卡住了。

I'll appreciate any help here. 在这里,我将不胜感激。

Here is my ajax: 这是我的ajax:

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;
  });
}

form php code: 表格php代码:

    <?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'] && isset($_POST['opta'])) {$extras= $extrasm .",". $extraso;}
if ($_POST['opt'] && !isset($_POST['opta'])) {$extras= $extrasm;}
if (!$_POST['opt'] && 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);

    }
}
?>

<form id="pre-process" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div style="background-color:#FFF; padding:20px;">
<?=$msg;?>
  <?php
  $name = getSqlField("SELECT name FROM products WHERE resid=".$_SESSION['rest_id']." and id=".$pid."","name");
  echo "<div style='color:#fff; background-color:#F00;padding:10px;' align='center'><h2>".$name."</h2></div><div style='background-color:#FFF; padding: 20px 70px 30px 70px; '>Please select accessories.<br><br>";
 $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 "<select name='opt[]' id='opt[]' style='width:220px;'>";
echo "<option value='' >Select Options</option>";
    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 "<option value=".$v." >".$aa."</option>";


    }
         echo "</select>(required)<br>";       
    //}
        }
        $getRss = mysql_query("SELECT * FROM optional_product where prodid=".$pid.""); 
        ?>
        <br><br>
        <table border="0" cellpadding="0" cellspacing="0" >
       <tr>
    <td bgcolor="#EAFFEC">
<div style="width:440px; "> 
<?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");
?>
<div style="width:180px;background-color:#EAFFEC; float:left;padding:10px;""><input type="checkbox" name="opta[]" id="opta[]" value="<?=$rssp['optid']?>"  /> <i><?=$optional?> [<?=CURRENCY?><?=$price?> ]</i> </div>
<?php } ?>
</div>

</td>
 </tr></table>
<input type="hidden" name="rid" value="<?=$rid?>" />
<input type="hidden" name="pid" value="<?=$pid?>"/>
    </div><input type="hidden" name="submit" /><input  id='submit' class="CSSButton"    style="width:120px; float:right;" name='submit' type='submit' value=' Continue ' /><br />

<br /><br />
</div>
</form>

I don't know colobox, but if I understand well what you are trying to do, 我不了解colobox,但是如果我很了解您要做什么,
I would say your javascript should more look like this 我会说您的JavaScript应该更像这样

function cbox_submit()
{
  jQuery("#pre-process").submit(function(e) {
    e.preventDefault(); // prevents the form to reload the page
    jQuery.post(
      jQuery(this).attr('action')
    , jQuery(this).serialize()
    , function(data) {
        if (data['ok']) { // ok variable received in json
          jQuery('#my_colorbox').colorbox.close(); // close the box
        }
      }
    );
    return false;
  });
}

jQuery(function() {
  jQuery('#my_colorbox').colorbox({
    maxWidth: '75%'
  , onComplete: cbox_submit // Bind the submit event when colorbox is loaded
  });
});

You should separate at least your php script that does the post part. 您应该至少分离出用于后期处理的php脚本。

And this php (called with jQuery(this).attr('action') ) should return a json ok variable if successfull. 如果成功,此php(用jQuery(this).attr('action')调用)应返回json ok变量。 Example: 例:

<?php
# ... post part ...
# if success
ob_clean();
header('Content-type: application/json');
echo json_encode(array('ok' => true));
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM