douchuoxuan3177 2015-01-20 21:45
浏览 51
已采纳

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

I have been given our intranet site to add a few features too, but I have very little experience with this kind of coding. (The company we had went out of business. Great). We are running a Drupal site and the following is some custom code to display an orange alert bar on the site (ie: Cookies in the lunch room!)

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(){});
}

Now, for the life of me, I can't figure out how to make it call a PHP function. They want it to make the title bar flash when a new alert is posted. I am trying to use this one: http://heyman.info/2010/sep/30/jquery-title-alert/ It works fine in small testing, but when I try to add it to their custom code I run into the following issues:

  1. I can't just add it to 'alertMsg' as that has a dataType of 'html' and just spits the PHP code out as text.
  2. I tried using $.post in a few spots and calling a file with the PHP function call, but nothing happened.
  3. I tried changing dataType to json, but then the alert just doesn't appear. I think there is more I would have to do here, but I have no idea.
  4. I tried to rewrite the body of the drupal node (in a drupal view) to include the PHP call, but the body will not execute PHP as well. I can't use the header or footer since the body is pulled out directly by the code above.

So, is there something obvious I am missing? I just want this call "$.titleAlert('New Alert');" to occur when a new alert is dropped.

Thanks so much!

  • 写回答

1条回答 默认 最新

  • dtz8044 2015-01-20 23:06
    关注

    If I am not mistaken, and understand correctly what you trying to achieve, you don't need PHP for this. Just make sure that you initialized Jquery Title Alert correclty, and replace this line

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

    with this

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

    if you would like to add the original alert text which appears, change the "Your message here!" to jQuery(this).find(".alert-msg").html()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?