0

我在fancybox 窗口中使用表单将(Ajax)数据发布到php 页面。

如果我在 Fancybox 之外运行表单,它会完美运行。插入 - 检查。响应 - 检查。也就是说,如果我通过 Fancybox 运行相同的页面,我会得到一个加载轮(在我关闭覆盖后仍然存在)。

在此处输入图像描述

表格(form_test.php):

<form id="form" method="post" action="">
    <input type="text" id="name" name="name" value="Test Name" />
    <input type="text" id="email" name="email" value="email@test.com" />
    <input type="submit" value="Login" />
</form>

<script type"text/javascript">
    $("#form").bind("submit", function () {
$.fancybox.showLoading(); // it was $.fancybox.showActivity(); for v1.3.4
$.ajax({
    type: "POST",
    cache: false,
    url: "test.php", // make sure your path is correct
    data: $(this).serializeArray(), // your were using $(form).serialize(), 
    success: function (data) {
        $.fancybox(data);
    }
});
return false;
}); // bind
</script>

PHP (test.php):

$name=$_POST['name'];
$email=$_POST['email'];

$query=mysql_query("INSERT INTO members (firstName,email) VALUES('$name','$email')");
if($query){
    echo "Data for $name inserted successfully!";
}
else{
    echo "An error occurred!";
}

想法?

4

1 回答 1

0

尝试

$("#form").bind("submit", function () {
    $.fancybox.showLoading(); // it was $.fancybox.showActivity(); for v1.3.4
    $.ajax({
        type: "POST",
        cache: false,
        url: "test.php", // make sure your path is correct
        data: $(this).serializeArray(), // your were using $(form).serialize(), 
        success: function (data) {
            $.fancybox(data);
        }
    });
    return false;
}); // bind

现在,$.fancybox(data);将返回(在 fancybox 内)您从text.php文件发送的任何内容,以便您可以<div id="message">在该文件中返回 from,例如:

if($query){
  echo "<div id='message'>Data for $name inserted successfully!</div>";
} else {
  echo "<div id='message'>An error occurred!</div>";
}
于 2013-03-02T02:59:24.430 回答