2014-02-11 55 views
0

我创建了一个使用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>&nbsp;</td></tr> 
     </table> 
    </form> 

回答

0

你在单独的文件或在同一页面的页脚包括本?

并尝试使用完整的网址mailer2.php文件。

尝试

get_stylesheet_directory() '/ mailer2.php'

0

你只需要使用完整路径mailer2.php 如果在主题文件夹中的应用:

url:<?php echo get_template_directory(); ?>'/mailer2.php' 

Ref:http://codex.wordpress.org/Function_Reference/get_template_directory

如果它在根目录下使用:

url:<?php echo site_url(); ?>'/mailer2.php' 

编号:http://codex.wordpress.org/Function_Reference/site_url

上述方法的工作只有这个脚本的PHP文件,如果你在一个.js添加这个脚本文件,您可以将网址直接加入到如文件:

url:'http://www.sireurl.com/path/to/mailer2.php' 
+0

我尝试使用'code'url:<?php get_template_directory()?>。'mailer2.php'和'code'url:get_template_directory()。'mailer2.php'既不工作,也不显示源地址只是代码 – BKCOHEN

+0

检查更新的答案,如果您正在使用'get_template_directory()'函数,您需要使用'echo'打印该值,它只返回url doesn'打印它像'the_content()' –

+0

我试过这个,甚至没有结果添加永久路径。 – BKCOHEN

相关问题