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条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测