gpt4 book ai didi

javascript - contact_me 表单的 WordPress AJAX

转载 作者:行者123 更新时间:2023-12-03 07:53:53 25 4
gpt4 key购买 nike

我已转换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'>&times;")
.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'>&times;")
.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com