[英]PHP + Javascript parent function not getting called
好了,我已经尝试了好几个小时了,但是我自己也解决不了。 我正在尝试从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
我在Google Chrome中按CTRL + Shift + I转到开发人员工具的网络>页面,该页面执行调用PHP函数的请求>预览,这就是我收到的信息: <script>parent.reloadprofmessages();</script>
但是,未调用该函数。 解决这个问题将为我解决许多问题,对我来说,实际上知道它为什么不起作用仍然是一个谜,因为它在其他情况下仍然有效。 提前谢谢你的帮助。
获取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();
}
}
希望能有所帮助
为脚本标签添加type属性
echo "<script type='text/javascript' >parent.reloadprofmessages();</script>";
并记住在此行之前定义javascript函数
所以这是哪里出了问题:(显示错误)
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
现在它可以工作了,谢谢您的帮助,我非常感谢,和往常一样,在这里提出要求后,我总是能找到一个更安全的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.