PHP + Javascript父函数未被调用

时间:2012-02-02 07:02:36

标签: php javascript jquery function parent

好吧我已经试图找到这个问题的答案已经好几个小时但我自己无法解决。 我试图从PHP函数调用Javascript父函数,但是,它没有被调用。 当使用onclick方法onclick='parent.dosomething();时,一切似乎都运行正常,但如果我尝试通过回显来调用该函数,那么它会因某种原因而失败。

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

这是PHP函数:

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
                }
            }
        }

    }
}

这是我父页面上的Javascript函数:

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

我在谷歌浏览器中按了CTRL + Shift + I来访问开发人员工具,网络&gt;执行调用PHP函数的请求的页面&gt;预览,这是我收到的: <script>parent.reloadprofmessages();</script> 但是,该功能未被调用。 解决这个问题可以解决很多问题,对我而言,知道为什么它不起作用实际上仍然是个谜,因为它在其他情况下起作用。 感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

获取javascript并使用AJAX执行它并不是一个好主意。我建议首先将PHP更改为:

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

然后将您的Javascript更改为:

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

希望有所帮助

答案 1 :(得分:0)

添加脚本标记

的type属性
echo "<script type='text/javascript' >parent.reloadprofmessages();</script>"; 

并记得在此行之前定义javascript函数

答案 2 :(得分:0)

所以这就是错误:(显示错误)

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" ); //$timeactivity was not being used
        $actualtime = time();

        $timetoseconds = $actualtime - $timeposted; //$timeposted doesn't even exist, in other words I wasn't even converting the $activity_date timestamp to time.
        $timetominutes = floor($timepassedtoseconds/60);

        if($timetominutes < 2)
        {
            if($activity_sender != $user)
            {
                if($activity_type == 1) //Messages
                {
                    echo "<script>parent.reloadprofmessages();</script>"; //this was not the correct way of calling a function from the parent page.
                }
            }
        }

    }
}

关于Javascript功能: 这是我结束的:

var auto_refresh = setInterval(
function reloadstring()
{
        $.get("checknewactivity.php?vprofile=<?php echo $actualuser; ?>", function(activity){
        if (activity == 1) 
        {
            $('#profcommentsdiv').load('showprofmessages.php?vprofile=<?php echo $actualuser; ?>').fadeIn("slow");
        }
        });
}, 1000); // refresh every 1000 milliseconds

现在它起作用了,谢谢你的帮助,我真的很感激,并且像往常一样,在这里提问后我总能得到一个更安全的解决方案。