PHP + Javascript 父函数没有被调用
好吧,我已经尝试寻找这个问题的答案几个小时了,但我自己无法解决。 我试图从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获取 javascript 并使用 AJAX 执行它并不是一个好主意。我建议首先将你的 PHP 更改为:
然后将你的 Javascript 更改为:
希望有帮助
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:
Then change your Javascript to this:
Hope that helps
添加 script 标签的 type 属性
,并记住在此行之前定义 javascript 函数
Add the type attribute for script tag
and remember to define the javascript function before this line
所以这是错误的:(显示错误)
关于 Javascript 函数:
这就是我的结论:
现在它可以工作了,谢谢您的帮助,我真的很感激,像往常一样,在这里询问后我总是能找到更安全的解决方案。
So here is what was wrong: (Showing errors)
About the Javascript function:
This is what I ended with:
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.