我创建了一个使用JQuery和AJAX提交表单和交换页面内容而不刷新页面的表单。这在WordPress以外测试时效果很好。当我添加到wordpress并填写表单时,页面只是刷新为空白表单。自定义HTML JQuery表单不能在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>
我尝试使用'code'url:<?php get_template_directory()?>。'mailer2.php'和'code'url:get_template_directory()。'mailer2.php'既不工作,也不显示源地址只是代码 – BKCOHEN
检查更新的答案,如果您正在使用'get_template_directory()'函数,您需要使用'echo'打印该值,它只返回url doesn'打印它像'the_content()' –
我试过这个,甚至没有结果添加永久路径。 – BKCOHEN