PHP + Javascript 父函数没有被调用

发布于 2025-01-02 02:11:04 字数 2140 浏览 2 评论 0原文

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

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

我在 Google Chrome 中按 CTRL + Shift + I 进入开发人员工具,网络 > 。执行调用 PHP 函数的请求的页面 >预览,这就是我收到的: 但是,该函数没有被调用。 解决这个问题会解决我很多问题,对我来说,知道为什么它不起作用实际上仍然是一个谜,因为它在其他情况下起作用。 提前感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街九尾狐 2025-01-09 02:11:05

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

希望有帮助

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

同尘 2025-01-09 02:11:05

添加 script 标签的 type 属性

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

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

Add the type attribute for script tag

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

and remember to define the javascript function before this line

迷乱花海 2025-01-09 02:11:05

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

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

现在它可以工作了,谢谢您的帮助,我真的很感激,像往常一样,在这里询问后我总是能找到更安全的解决方案。

So here is what was wrong: (Showing errors)

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

    }
}

About the Javascript function:
This is what I ended with:

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

And now it works, thank you for your help, I really appreciate it, and as usual, I always get to a safer solution after asking it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文