2012-02-02 78 views
0

好吧我一直在試圖找到一個答案已經幾個小時了,但我無法自己解決它。 我想從PHP函數調用Javascript父函數,但是,它沒有被調用。 當使用onclick方法onclick='parent.dosomething();一切似乎工作正常,但如果我嘗試通過echo'ing它來調用該函數,它會因某種原因失敗。PHP + Javascript的父功能沒有被調用

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 +我在谷歌瀏覽器去開發人員工具「網絡」>執行調用PHP函數>預覽的請求的頁面,這是我收到的內容: <script>parent.reloadprofmessages();</script> Howe ver,函數沒有被調用。 解決這個問題會解決我很多問題,對我來說,知道爲什麼它不起作用仍然是個謎,因爲它在其他情況下起作用。 非常感謝您的幫助。

+1

我不會嘗試使用這種調用JavaScript與PHP的方法。如果你需要多次調用這個js方法會發生什麼?您不希望繼續回顯

0

添加類型屬性腳本標籤

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

並記得在此行之前定義javascript函數

+0

我可以解決我的問題,但謝謝。 – 2012-02-02 08:40:15

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 

而現在它的作品,謝謝你你的幫助,我真的很感激它,和往常一樣,我總是在問這裏後得到一個更安全的解決方案。