因此,我很难将我的联系方式设置为我正在构建的主题。
它使用JqBootstrapValidation插件。
有人告诉我,这不起作用的原因是,当js通过AJAX调用时,WP没有被加载contact-me.php
文件因此,如果有人能在此基础上实现WP-ajaxapi,我将非常感激,因为我一点都不知道。
顺便说一句,JS文件工作得很好。正在验证字段,我在发送表单时收到一条成功消息。
以下是Js代码:
(function($) {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get theme path
var stylesheet_directory_uri = ContactMe_uri.templateUrl;
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var subject = $("input#subject").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(\' \') >= 0) {
firstName = name.split(\' \').slice(0, -1).join(\' \');
}
$.ajax({
url: stylesheet_directory_uri+"/inc/contact-me.php",
type: "POST",
data: {
name: name,
subject: subject,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$(\'#success\').html("<div class=\'alert alert-success\'>");
$(\'#success > .alert-success\').html("<button type=\'button\' class=\'close\' data-dismiss=\'alert\' aria-hidden=\'true\'>×")
.append("</button>");
$(\'#success > .alert-success\')
.append("<strong>Your message has been sent. </strong>");
$(\'#success > .alert-success\')
.append(\'</div>\');
//clear all fields
$(\'#contactForm\').trigger("reset");
},
error: function() {
// Fail message
$(\'#success\').html("<div class=\'alert alert-danger\'>");
$(\'#success > .alert-danger\').html("<button type=\'button\' class=\'close\' data-dismiss=\'alert\' aria-hidden=\'true\'>×")
.append("</button>");
$(\'#success > .alert-danger\').append("<strong>Sorry " + firstName + ", it seems that mail server is not responding. Please try again later!");
$(\'#success > .alert-danger\').append(\'</div>\');
//clear all fields
$(\'#contactForm\').trigger("reset");
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\\"tab\\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
}(jQuery));
/*When clicking on Full hide fail/success boxes */
jQuery(\'#name\').focus(function() {
jQuery(\'#success\').html(\'\');
});