douzhoubing2805 2012-02-02 07:02
浏览 82
已采纳

PHP + Javascript父函数未被调用

Alright I've been trying to find an answer to this for hours already but I couldn't resolve it myself. I'm trying to call a Javascript parent function from a PHP function, however, it is not getting called. When using the onclick method onclick='parent.dosomething(); everything seems to work fine but if I try to call the function by echo'ing it out, it would just fail for some reason.

echo "<script>parent.reloadprofmessages();</script>"; //this is what is not getting called

Here's the PHP function:

function checkactivity($username)
{
    //These are just queries being executed (irrelevant)

    $querystats = "SELECT users.fullname, activity.id, activity.sender, activity.receiver, activity.type, activity.dateposted, activity.seen, activity.related FROM activity, users WHERE activity.receiver = '$username' && activity.seen = '0' ORDER BY id DESC LIMIT 1";
    $resultstats = mysql_query($querystats);
    $num_stats = mysql_num_rows($resultstats);
    $rowactivity = mysql_fetch_assoc($resultstats);

    //End of queries

    if($num_stats > 0) //If there are registries
    {
        $user = $_SESSION['Username'];

        $activity_date = $rowactivity["dateposted"];
        $activity_type = $rowactivity["type"];
        $activity_sender = $rowactivity["sender"];
        $timeactivity = strtotime( "$activity_date" );
        $actualtime = time();

        $timetoseconds = $actualtime - $timeposted; 
        $timetominutes = floor($timepassedtoseconds/60);

        if($timetominutes < 2)
        {
            if($activity_sender != $user)
            {
                if($activity_type == 1) //Messages
                {
                    echo "<script>parent.reloadprofmessages();</script>"; //this is what is not getting called
                }
            }
        }

    }
}

And this is my Javascript function at the parent page:

function reloadprofmessages()
{
    $('#friendrequests').load('showprofmessages.php?username=<?php echo $actualuser; ?>').fadeIn("slow");
} //refreshes messages

I pressed CTRL + Shift + I in Google Chrome to get to the developer tools, Network > page that does the request that calls the PHP function > Preview and this was what I received: <script>parent.reloadprofmessages();</script> However, the function is not getting called. Resolving this would solve me a lot of problems, to me it is actually still a mystery to know why it doesn't work since it has worked in other cases. Thank you for your help in advance.

  • 写回答

3条回答 默认 最新

  • dongwei1234 2012-02-02 07:10
    关注

    It's not a good idea to fetch javascript and execute it with AJAX. What I would suggest is to firstly change your PHP to this:

    if($activity_type == 1) //Messages
    {
        echo "1";
    }
    else {
        echo "0";
    }
    

    Then change your Javascript to this:

    function reloadprofmessages()
    {
        var can_reload = $.ajax({ url: "showprofmessages.php?username=<?php echo $actualuser; ?>" });
        if (can_reload) {
            parent.erloadprofmessages();
        }
    }
    

    Hope that helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行