javascript - Wordpress - 发布到自定义 PHP 文件 - 错误 500

标签 javascript jquery html ajax wordpress

我们的网站使用 WordPress。我被要求在我们的时事通讯订阅中添加一个功能,该功能可以根据表单的选定值自动将电子邮件发送到特定地址。从代码端和我的本地主机上工作正常,但是当将其实现到实时 WordPress 系统中时,我遇到了错误。情况:

jQuery.AJAX 脚本将表单数据发送到 wp-content 文件夹中的文件“mail.php”。然后,AJAX 成功函数会提交原始表单(因为数据还需要发布到管理我们的时事通讯订阅的提供商)。这在非 WordPress 本地主机上运行良好。

在搜索 javascript 控制台和 firebug 后,我意识到在脚本尝试将数据发布到 email.php 后,服务器返回 500 错误,就好像它不允许将数据发布到此文件一样。

我没有以任何方式注册 mail.php 或脚本,而是将其添加到电子邮件表单后面的 html 代码中。我在这里错过了什么吗?

谢谢!

<script>
jQuery(document).ready(function() {
    jQuery( "#subscribeform" ).one( "submit", function(event) {
        event.preventDefault();
        var pFirstName = jQuery("#firstname").val();
        var pLastName = jQuery("#name").val();
        var pSalutation = jQuery("#salutation").val();
        var peMail = jQuery("#email").val();
        var pDOB = jQuery("#dob").val();
        var pMailTo = jQuery("#shop").val();

        var data = {
            firstname: pFirstName,
            name: pLastName,
            salutation: pSalutation,
            email: peMail,
            dob: pDOB,
            mailto: pMailTo
        };

        $.ajax({
            type: "POST",
            url: "/cms/mail.php",
            data: data,
            success: function(){
                jQuery('#subscribeform').attr('action', "theExternalProviderURL").submit();
            }
        });
    });
});
</script>

ma​​il.php

<?php

include_once '/cms/phpmailer/PHPMailerAutoload.php';

if($_POST){
    $shopname = $_POST['mailto'];
    $salutation = $_POST['salutation'];
    $firstname = $_POST['firstname'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $dateofbirth = $_POST['dob'];
    $recipient = $_POST['mailto'];

    switch ($recipient) {
        case "Value1":
            $recipient = "mail1@mail.com";
            break;
        case "Value2":
            $recipient = "mail2@mail.com";
            break;
        default:
            $recipient = "admin@mail.com";
    }

    $oMailer = new PHPMailer;
    $oMailer->CharSet = 'UTF-8'; 
    $oMailer->isSMTP();
    $oMailer->Host = 'mail.host.com';
    $oMailer->Username = 'xxx';
    $oMailer->Password = 'xxx';
    $oMailer->SMTPAuth = true;
    $oMailer->SMTPSecure = 'tls';
    $oMailer->Port = 587; 
    $oMailer->From = 'email@email.com';
    $oMailer->FromName = 'From Email';
    $oMailer->addAddress('adress@adress.com'); 
    $oMailer->isHTML( true );
    $oMailer->Subject = 'E-Mail Subject';
    $oMailer->Body = 'Text Text Text';
    $oMailer->AltBody = strip_tags( $oMailer->Body );
    $oMailer->SMTPDebug = 2;

    if ( !$oMailer->send() ) {

        echo "Error sending Mail: " . $oMailer->ErrorInfo;
        exit;

    }
    echo 'Successfully sent mail to ' . $recipient . ' Shop'; 

}

?>

最佳答案

如前所述,HTTP 500 来自您的 server/mail.php 代码中的问题。此外,WP 中有一个特殊的钩子(Hook)可以处理 ajax 请求,请参见此处:https://codex.wordpress.org/AJAX_in_Plugins

您需要的是:

 var data = {data:yourdata, action: "yourajaxaction"};
 $.post(ajaxurl,{data: data});

add_action( 'wp_ajax_yourajaxaction', 'your_action' );

function your_action() { 
   include "mail.php";
}

关于javascript - Wordpress - 发布到自定义 PHP 文件 - 错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49554229/

相关文章:

javascript - 如何实现 CSS3 GWT TransitionEnd 监听器

javascript - 为什么onClick事件会触发两次?

javascript - 使用 javascript 验证单选按钮

html - 表格内的CSS水平滚动

javascript - 如何使用 JQuery 将 H1 元素与另一个文档中的 H1 交换

javascript - 将数据插入二维数组中特定索引处的数组中

jquery - 如何在谷歌翻译脚本中设置默认语言

jquery - 奇怪的 jQuery 悬停行为

javascript - 使用 coffeescript/jQuery 生成 HTML 的方法比使用字符串更好吗?

html - 我怎样才能使 :hover area of effect inside the element with the property?