编辑 Jquery Ajax 函数以调用 PHP 函数

Editing Jquery Ajax function to call PHP function

我也被要求在我们的内部网站上添加一些功能,但我对这种编码经验很少。 (我们的公司倒闭了。太好了)。我们正在 运行 建立一个 Drupal 网站,以下是一些自定义代码,用于在网站上显示橙色警告栏(即:午餐室里的 Cookie!)

function alert_poll() {
    setTimeout(function(){
        jQuery.ajax({ 
            url: baseUrl + "intranet/alerts/get",
            success: function(data){
                alert_setup(data);
            }, 
            dataType: "html"

        });
    }, 60000); // Polling every 60 seconds

}

// Setup
function alert_setup(data) {
    jQuery(data).find(".views-row").each(function() {
        var alertId = jQuery(this).find(".alert-nid").text();
        var alertMsg = "<span class='alert-label'>** ALERT MESSAGE **</span> <span class='alert-msg'>";
        alertMsg += jQuery(this).find(".alert-msg").html() + "</span>";
        alertMsg += "<a href='#' class='alert-hide'><span>Hide Alert</span></a>";
        if ( !jQuery.cookie("scl-alert-" + alertId) ) {
            // Loop through all on-page alerts to see if this one is currently on the page already
            // if so, don't display
            var displayAlert = true;
            jQuery(".intranet-alert-wrap").each(function() {
                var onpageAlertId = jQuery(this).attr("id");
                if ( onpageAlertId.indexOf("-" + alertId) ) { displayAlert = false; }
            });
            if ( displayAlert ) { inject_alert(alertMsg,alertId); }
        }
    });
    alert_poll(); //Setup the next poll recursively
}

// Drop alert message onto page
function inject_alert(alert_msg,alert_id) {
    jQuery("#zone-content").prepend("<div class='intranet-alert-wrap' id='alert-" + alert_id + "' style='opacity:0;'><div class='intranet-alert'>" + alert_msg + "</div></div>");
    jQuery(".intranet-alert-wrap").stop().animate( { "opacity": 1}, 700, null, function(){});
}

现在,对于我来说,我想不出如何让它调用 PHP 函数。他们希望它在 post 编辑新警报时使标题栏闪烁。我正在尝试使用这个:http://heyman.info/2010/sep/30/jquery-title-alert/ 它在小型测试中工作正常,但是当我尝试将它添加到他们的自定义代码时,我 运行 遇到以下问题:

  1. 我不能只将它添加到 'alertMsg',因为它的数据类型为 'html',并且只是将 PHP 代码作为文本输出。
  2. 我尝试在几个地方使用 $.post 并使用 PHP 函数调用调用一个文件,但没有任何反应。
  3. 我尝试将 dataType 更改为 json,但随后就没有出现警告。我想我还需要在这里做更多的事情,但我不知道。
  4. 我试图重写 drupal 节点的 body(在 drupal 视图中)以包含 PHP 调用,但是 body 不会执行 PHP以及。我不能使用 header 或页脚,因为 body 是由上面的代码直接拉出的。

那么,我是否遗漏了一些明显的东西?我只想要这个调用“$.titleAlert('New Alert');”在删除新警报时发生。

非常感谢!

如果我没记错,并且正确理解了您要实现的目标,那么您不需要 PHP。只需确保您已正确初始化 Jquery Title Alert,并替换此行

if ( displayAlert ) { inject_alert(alertMsg,alertId); }

有了这个

   if ( displayAlert ) { inject_alert(alertMsg,alertId); $.titleAlert("Your message here!");}

如果您想添加显示的原始警报文本,请将 "Your message here!" 更改为 jQuery(this).find(".alert-msg").html ()