簡體   English   中英

用於contact_me表單的WordPress AJAX

[英]WordPress AJAX for contact_me form

我已將此模板轉換為WordPress主題。 一切工作正常,除了在contact_me.js部分中無法獲取AJAX請求的正確網址。

 $.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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM