使用Fancybox 2发布数据

时间:2013-03-02 02:13:45

标签: php jquery ajax fancybox-2

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

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

enter image description here

表格(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!";
}

想法?

1 个答案:

答案 0 :(得分: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">,如:

if($query){
  echo "<div id='message'>Data for $name inserted successfully!</div>";
} else {
  echo "<div id='message'>An error occurred!</div>";
}