AJAX Contact Form Issue

时间:2011-05-10 作者:joshvermaire

我为我的侧边栏创建了一个联系人表单,它在Chrome中工作得很好,但在IE或FF中却没有。

我已经盯着它看了好几个小时了,没发现我做错了什么。

我在任何地方都没有收到任何错误,但当我在FF或IE中提交它时,它会在控制台中记录“nope”,并在#jvmsg中回显一条-1的消息。

我的html是:

<li id="jvcontactwidget-3" class="widget widget_contact">
  <div class="title"><h2>Test</h2></div>
  <form id="jvcontact" name="widget_contact" action="" method="post">
    <label class="required" for="email">Email Address (required)</label>
    <input class="required" type="email" name="email">
    <label class="required" for="firstName">First Name (required)</label>
    <input class="required" type="text" name="firstName">
    <label class="required" for="lastName">Last Name (required)</label>
    <input class="required" type="text" name="lastName">
    <label for="question">Any questions for us?</label>
    <textarea rows="5" name="question"></textarea>
    <input type="submit" value="Send it" style="display: inline-block; ">
</form>
<div class="jvloading" style="display: none; "></div>
<div id="jvsuccess" style="display: none;" >Thanks for the email</div>
<div id="jvmsg" style="display: none; "></div>
</li>
我的js文件如下:

jQuery(document).ready(function($) {
jQuery(\'#jvcontact\').submit(function() {
    jQuery(\'.widget_contact input[type="submit"]\').hide();
    jQuery(\'.jvloading\').show();
    var str = jQuery(this).serialize();
    jQuery.ajax({
        type: \'POST\',
        url: \'/wp-admin/admin-ajax.php\',
        data: \'action=contact_form&\'+str,
        success: function(msg) {
            //jQuery(\'#jvmsg\').ajaxComplete(function(event, request, settings){
                if(msg === \'sent\') {
                    console.log(\'sent\');
                    jQuery(\'#jvmsg\').hide();
                    jQuery(\'.jvloading\').slideUp();
                    jQuery(\'.widget_contact input[type="submit"]\').slideUp();
                    jQuery(\'#jvcontact\').slideUp();
                    jQuery(\'#jvsuccess\').fadeIn(\'slow\');
                }
                else {
                    console.log(\'nope\');
                    jQuery(\'#jvmsg\').html(msg);
                    jQuery(\'.jvloading\').hide();
                    jQuery(\'.widget_contact input[type="submit"]\').show();
                    jQuery(\'#jvmsg\').fadeIn(\'slow\');
                }
            //});
        }
    });
    return false;
});
});

我的php文件:

function ajax_contact() {
if(!empty($_POST)) {

  $error = "";

  //Check to make sure sure that a valid email address is submitted
  if(trim($_POST[\'email\']) == \'\')  {
    $error .= "An email address is required<br/>";
  } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$", trim($_POST[\'email\']))) {
    $error .= "A valid email address is required<br/>";
  } else {
    $email = trim($_POST[\'email\']);
  }

  //Check to make sure that the first name field is not empty
  if(trim($_POST[\'firstName\']) == \'\') {
    $error .= "Your first name is required<br/>";
  } else {
    $firstName = trim($_POST[\'firstName\']);
  }

  //Check to make sure that the last name field is not empty
  if(trim($_POST[\'lastName\']) == \'\') {
    $error .= "Your last name is required<br/>";
  } else {
    $lastName = trim($_POST[\'lastName\']);
  }

  if(trim($_POST[\'question\']) == \'\') {
    $question = "They had no questions at this time";
  } else {
    $question = $_POST[\'question\'];
  }

  $admin_mail = get_bloginfo(\'admin_email\');


if(empty($error)) {

  $message  = "Someone wants to get in touch with you. \\n\\n
               Name: " . $firstName . " " . $lastName . "\\n
               Email: " . $email . "\\n
               Question: " . $question . "\\n";

  $jvMail = wp_mail($admin_mail, \'Contact from your website\', $message);
  echo "sent";

} else {
  echo $error;
}
die();
}
}

add_action(\'wp_ajax_contact_form\', \'ajax_contact\');
谢谢你的帮助!

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您是否使用所有浏览器登录?自从

add_action(\'wp_ajax_contact_form\', \'ajax_contact\');

仅适用于登录用户,因此要修复它,您需要更改它:

add_action(\'wp_ajax_nopriv_contact_form\', \'ajax_contact\');

如果你想让你的功能为访问者和用户服务。

结束

相关推荐

无法访问通过AJAX调用的文件中的WordPress函数?

我正在尝试使用jQuery重新加载新帖子。据我所知,我无法在页面中重新加载div的内容,所以我将文件重新加载到该div中。问题是我加载的文件给了我Fatal error: Call to undefined function wp_query()如何对主题目录中新创建的文件实现功能以正常工作?This is my jQuery <script language=\"JavaScript\"> $(function() { var SANAjax = function()