Ajax POST不工作$ _POST

时间:2014-01-17 07:25:35

标签: php jquery html ajax

我正在尝试通过ajax向php文件提交联系表单。我使用phpMailer发送表单结果。然而,POST似乎不起作用。当我提交时,成功或错误条件都不会执行。当我注释掉POST变量时,ajax请求成功执行,电子邮件发送(变量空白)。这是代码......

<form  method="post" class="form2" id="form2Id" name="form" action="">

            <div class="formtitle">Form<</div>

            <div class="input">
                <div class="inputtext">Your Name: </div>
                <div class="inputcontent">

                    <input id="nameId" type="text" name="name" />

                </div>
            </div>

            <div class="input">
                <div class="inputtext">Your Phone Number: </div>
                <div class="inputcontent">

                    <input id="phoneNumberId" type="text" name="phoneNumber" />

                </div>
            </div>

            <div class="input">
                <div class="inputtext">Your Email Address: </div>
                <div class="inputcontent">

                    <input id="emailAddressId" type="text" name="emailAddress"/>

                </div>
            </div>

            <div class="inputtextbox nobottomborder">
                <!--<div class="inputtext">Message: </div>-->
                <div class="inputcontent">

                    <!--<textarea id="messageId" class="textarea" name="message"></textarea>-->

                </div>
            </div>

            <div class="buttons">

                <input class="orangebutton" type="submit" id="formSubmit" value="Submit" />

            </div>

        </form>

<form method="post" class="form2" id="form2Id" name="form" action=""> <div class="formtitle">Form<</div> <div class="input"> <div class="inputtext">Your Name: </div> <div class="inputcontent"> <input id="nameId" type="text" name="name" /> </div> </div> <div class="input"> <div class="inputtext">Your Phone Number: </div> <div class="inputcontent"> <input id="phoneNumberId" type="text" name="phoneNumber" /> </div> </div> <div class="input"> <div class="inputtext">Your Email Address: </div> <div class="inputcontent"> <input id="emailAddressId" type="text" name="emailAddress"/> </div> </div> <div class="inputtextbox nobottomborder"> <!--<div class="inputtext">Message: </div>--> <div class="inputcontent"> <!--<textarea id="messageId" class="textarea" name="message"></textarea>--> </div> </div> <div class="buttons"> <input class="orangebutton" type="submit" id="formSubmit" value="Submit" /> </div> </form>

$( "#formSubmit" ).click(function() {
$("#form2Id").validate({
debug: true,
 submitHandler: function(form) {

$.ajax({
 type: "POST",
 url: "handler.php",
 data: $(form).serialize(),
 timeout: 12000,
 success: function() { alert("Success"); },
  error: function() { alert("FAILURE"); }
 });
return false;
}
});
});

)};

$( "#formSubmit" ).click(function() { $("#form2Id").validate({ debug: true, submitHandler: function(form) { $.ajax({ type: "POST", url: "handler.php", data: $(form).serialize(), timeout: 12000, success: function() { alert("Success"); }, error: function() { alert("FAILURE"); } }); return false; } }); }); )};

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

鉴于html行<input id="nameId" type="text" name="name" />和此php代码$nameId = $_POST['nameId'];,您的确会以空$nameId结尾,因为发布的值在$_POST['name']。您的其他变量也是如此。

答案 1 :(得分:0)

如果我发表评论,

$nameId = $_POST['name'];
$phoneNumberId = $_POST['phoneNumber'];
$emailAddressId = $_POST['emailAddress'];

$nameId = $_POST['name']; $phoneNumberId = $_POST['phoneNumber']; $emailAddressId = $_POST['emailAddress'];

只要我输入有效的电子邮件地址,电子邮件就会成功发送。否则,电子邮件根本不发送。在这两种情况下,ajax都会返回成功的响应。我已经尝试过使用名称而不是id的上述建议,但它不起作用。