【问题标题】:php file to load on click buttonphp 文件在点击按钮上加载
【发布时间】:2016-09-03 10:20:06
【问题描述】:

我正在点击事件加载personalizetest.php(它有用于发送电子邮件的 swiftmailer 代码)。当我通过单击带有 jquery 代码的 html 文件中的按钮来加载它时,它可以工作,并发送一封电子邮件。但是,当我在 WordPress 的 php 文件中添加相同的代码并单击按钮时,它在控制台中显示“操作已成功执行”,但我没有收到任何电子邮件。

我搜索了一下,发现我必须在 function.php 中添加以下代码才能在 Wordpress 上执行我所做的文件,但现在它在每次页面刷新时发送电子邮件。我要么打开网站,要么从一个页面转到另一个页面,而不是单击按钮。简而言之,当我单击带有class=button 的按钮时,我正在加载这个personalizetest.php 文件:

函数.php

<?php

function enqueue_scripts_styles_init() {


wp_enqueue_script( 'ajax-script', get_template_directory_uri().'./personalizetest.php', array('jquery'), 1.0 ); // jQuery will be included automatically
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
 }

  add_action('init', 'enqueue_scripts_styles_init');
?>

jQuery 代码

$(document).ready(function(){
$('.button').click(function(){
    var clickBtnValue = $(this).val();
    var ajaxurl = 'personalizetest.php',
    data =  {'action': clickBtnValue};
    $.post(ajaxurl, data, function (response) {
        // Response div goes here.
        console.log("action performed successfully");
    });
});

<input type="submit" class="button" name="insert" value="insert" />

personalizetest.php(Swiftmailer 代码)

 <?php

 require_once '/lib/config.php';
 require_once 'MyDecoratortest.php';


 function send_my_mail() {

 try {

//connect to database
$conn = new mysqli($root, $dbun, $dbps, $dbnm);

//get receipents
$recipients =[];
$sql = 'SELECT email, firstname, order_id,  FROM order_details order by _id desc limit 0, 1'; 
$result = $conn->query($sql);
$i = 0;
while ($row = $result->fetch_assoc()) {
     $recipients[$i]['email'] = $row['email'];
     $recipients[$i]['firstname'] = $row['firstname'];
     $recipients[$i]['order_id'] = $row['order_id'];
     $i++;    

 }


// create the transport
$transport = Swift_SmtpTransport::newInstance($smtp_server, 587, 'tls')
    ->setUsername($username)
    ->setPassword($password);
$mailer = Swift_Mailer::newInstance($transport);

// create and register decorator
$decorator = new Swift_Plugins_DecoratorPlugin(new MyDecorator($conn));
$mailer->registerPlugin($decorator);

//email
$html_message="My email text";

// prepare email message
$message = Swift_Message::newInstance()
    ->setSubject('Your Order at Dissertation Sage -- #order_id')
    ->setFrom($from)
    ->setBcc($bcc)
    ->setBody($html_message, 'text/html');


// tracking variables
$sent = 0;
$failures = [];

// send the personalized message to each recipient
foreach ($recipients as $recipient) {
    $message->setTo([$recipient['email'] => $recipient['firstname']]);
    $sent += $mailer->send($message);
  }

// display result
if ($sent) {
    echo "Number of emails sent: $sent<br>";
}
if ($failures) {
    echo "Couldn't send to the following addresses:<br>";
    foreach ($failures as $failure) {
        echo $failure . '<br>';
    }
    }
} catch (Exception $e) {
echo $e->getMessage();
}}?>

【问题讨论】:

  • 感谢 StepUp 的更正。我是新手,英语不好。我会尽量按照你的格式写。

标签: php jquery ajax wordpress


【解决方案1】:

首先,您将.php 包含在您的wp_enqueue_script 中。这是行不通的。如果您的 ajax 回调函数在 personalizetest.php 文件中,则需要使用

包含它
<?php

require_once( get_template_directory(). '/{your folder name here}/personalizetest.php' );

add_action('init', 'enqueue_scripts_styles_init');

function enqueue_scripts_styles_init() {
    wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/custom.js', array('jquery'), 1.0 ); // jQuery will be included automatically
    wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
 }

?>

此处排队的脚本称为 custom.js,但您可以将其设置为您用来调用 ajax 调用的任何脚本。

js 部分应如下所示(在您的 custom.js 等效项中):

jQuery(document).ready(function($){
$('.button').click(function(){
    var ajaxurl = ajax_object.ajaxurl,
    data =  {'action': 'my_action'};
    $.post(ajaxurl, data, function (response) {
        // Response div goes here.
        console.log("action performed successfully");
    });
});

动作应该使用钩子指向位于personalizetest.php文件中的ajax回调函数:

add_action( 'wp_ajax_my_action', 'my_action_callback' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );

(更改c的动作名称)。

你可以阅读我写的关于 ajax 加载的博文,它是针对帖子的,但同样的原则也适用于这里:

AJAX load posts on WordPress

【讨论】:

  • 感谢 Loic 和 dingo_d。你们俩确实是新编码员的帮手...我不知道我是否做得正确,但我需要您的帮助。我已经粘贴了我在personalizetest.php 中的代码供您阅读。
猜你喜欢
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2015-02-27
  • 2022-07-21
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多