【发布时间】:2014-02-11 02:19:40
【问题描述】:
我创建了一个表单,它使用 JQuery 和 AJAX 提交表单并交换页面内容,而无需刷新页面。在 WordPress 之外进行测试时,这非常有效。当我添加到 wordpress 并填写表单时,页面只是刷新为空白表单。
jQuery
$(document).ready(function() {
//When the form is submitted...
$('#savvyform').on('submit',function(e) {
//Send the serialized data to mailer.php.
$.ajax({
url:'mailer2.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(5000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(5000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
//$.post("mailer.php");
//Take our response, and replace whatever is in the "form2"
//div with it.
$('#form1').hide();
$('#form2').show();
});
});
这是 HTML 表单
<form id="savvyform" action="" method="POST" autocomplete="on">
<table>
<tr><td>First Name*:</td><td><input type="text" name="formfname" autofocus required></td><td>Address:</td><td><input type="text" name="formaddress"></td></tr>
<tr><td>Last Name*:</td><td><input type="text" name="formlname" required></td><td>City:</td><td><input type="text" name="formcity"></td></tr>
<tr><td>E-mail*:</td><td><input type="email" name="formemail" required></td><td>State/Province:</td><td><input type="text" name="formstate"></td></tr>
<tr><td>Phone*:</td><td><input type="text" name="formphone" required></td><td>Zip:</td><td><input type="text" name="formzip"></td></tr>
<tr><td colspan="2">Brief description of goals:</td><td>Country:</td><td><input type="text" name="formcountry"></td></tr>
<tr><td colspan="2"><textarea name="formgoals"></textarea></td></tr>
<tr><td><input type="submit" name="submit" value="Submit" onMouseDown="javascript:swapContent('form2');"></td><td> </td></tr>
</table>
</form>
【问题讨论】: