作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已转换this template进入 WordPress 主题。一切正常,除了我无法在 contact_me.js 中获取 AJAX 请求的正确 url。部分。
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
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 my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
},
我知道这与 WordPress 中文件的引用方式有关。我在 HTML 中添加 js 文件时做了类似的事情,但我不知道如何在 js 文件中处理它。
编辑:也许我必须添加 contact_me.php 中的代码到functions.php 中的ajax 回调?
编辑2这是我的 send_form,它似乎没有发送任何邮件。
function send_feedback(){
// Check for empty fields
if(empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$email_address = $_POST['email'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'xyz@mydomain.io'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Demo Feedback Form: $email_address";
$email_body = "You have received a new message from your demo feedback form.\n\n"."Here are the details:\n\nEmail: $email_address\n\nMessage:\n$message";
$headers = "From: noreply@mydomain.io\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
}
add_action('wp_ajax_nopriv_send_feedback', 'send_feedback');
add_action('wp_ajax_send_feedback', 'send_feedback');
最佳答案
添加到 header.php
<script>var ajaxURL = '<?php echo esc_js(admin_url('admin-ajax.php'));?>';</script>
添加您的网址:ajaxURL
就你的情况
$.ajax({
url: ajaxURL,
type: "POST",
data: {
action: 'send_form',
name: name,
phone: phone,
email: email,
message: message
},
在你的functions.php中
function send_form(){
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
//Do your send mail function etc.
wp_die();
}
add_action('wp_ajax_nopriv_send_form', 'send_form');
add_action('wp_ajax_send_form', 'send_form');
关于javascript - contact_me 表单的 WordPress AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880092/
我正在使用带有 contact_me.js 和 contact_me.php 的 Bootstrap contact_form 来提交我的联系表单。我遇到的问题是我有一个单页网站,页面上方是一个 Pa
我已转换this template进入 WordPress 主题。一切正常,除了我无法在 contact_me.js 中获取 AJAX 请求的正确 url。部分。 $.ajax({
我是一名优秀的程序员,十分优秀!