获取html表单信息并使用ajax将其推送到PHP


get html form info and push to PHP using ajax

我很难处理这个ajax getPOST。我尝试了各种方法来获取用户信息并将其发送到一个虚假的服务器(我的php文件),包括

序列化表单数据,

将action="reserveA.php"和method="post"添加到表单中申报,

ajax中的数据等于表单输入id。val()

我确信我在其他地方搞砸了,尽管我有一个php页面,我正在引用,但我在页面上没有任何东西,因为我不知道该放什么。

HTML表单

<form  id="thisform" action="reserveA.php" method="post">
    <fieldset>
      <legend>Place a Reservation</legend>
      <label for="firstName">First name: </label>
      <input id="firstName" type="text" placeholder="first name" autofocus="autofocus"
        required="true" /><br/><br/> 

以结束

<input id="submit" type="submit" value="Submit"/>

JS

$(document).ready(function() {            
    var submit = document.getElementById("submit");
    var firstName = $("#firstName").val();
    var LastName = $("#LastName").val();
    var Phone = $("#Phone").val();
    var Party = $("#Party").val();
    var dateof = $("#dateof").val();
    var Timeof = $("#Timeof").val();
    submit.onclick = function(){         
        $.ajax({
            url: "reserveA.html",
            dataType: "json",
            data: $("thisform").serialize(),
            type: "POST",
            success: function() {
                console.log("oh")
                alert("form submitted");
            },
        });
    }
}

则我的CCD_ 1为空。

您有一些错误,但基本上您的AJAX调用应该是这样的:

$.ajax({
    url: $("#thisform").attr('action'),
    dataType: "json",
    data: $("#thisform").serialize(),
    type: $("#thisform").attr('method'),
    success: function() {
        console.log("oh")
        alert("form submitted");
    },
});

如果你想了解更多,请查看以下帖子:

PHP+AAJAX

调试AJAX