使用 jquery ajax 序列化数据并提交到 mysql 数据库中


Using jquery ajax to serialize data and submit into mysql database

我遇到的问题是,每当将数据插入数据库时,它都不会将用户重定向到发票.php页面。拜托伙计们,我真的需要你们的帮助。

这是 html 代码:

<form method="POST" action="">
<input type="text" name="resident_address" id="resident_address"/>
<input type="text" name="price" id="status"/>
<input type="hidden" name="status" id="status" value="0"/>
</form>

这是jquery代码:

var dataString = $('#applyform').serialize();
$.ajax({
                        type: "POST",
                        url: "applyform.php",
                        cache: false,
                        data: dataString,
                        beforeSend: function()
                        {
                            $(".apply_error").hide();
                        },
                        success: function(html) {
                            if (html == "true")
                            {
                                // You can redirect to other page here....
                                window.location.href = 'invoice.php';
                            }
                            else
                            {
                                //window.location.href = 'apply.php';
                                $("div.apply_error").html("Wrong details").show();
                            }
                        }                
                    }); 

这是PHP,它是申请表.php:

if(isset($_POST['Submit']))
{
   $result = mysql_query("INSERT INTO mytable (resident_address, price, status) VALUES ('$addressfields', '$price', '$status')");
   if($result){
      echo "true";
   }
}

您没有发布一个名为"提交"的POST变量,因此您的

if(isset($_POST['Submit']))

将始终计算为false并且您的 MySQL 查询永远不会执行。