通知脚本,如何将数据放入div中

时间:2011-04-30 21:16:32

标签: php jquery mysql notifications

我尝试使用以下代码为网站创建通知系统:

setInterval(function() {
$.post('notifications.php', {

    email: 123

}, function(data) {


});
}, 30000);

和notifications.php:

$userid = $_SESSION[username];
$notifications = array();
$sql = "SELECT appreciation FROM ms_notifications WHERE email = '$userid' AND new = '1'";
$res = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($res)) {
while ($r = mysql_fetch_object($res)) {
    $notifications[] = $r->appreciation;
}
$nb_result = mysql_num_rows($res);
}
$sql = "UPDATE ms_notifications SET new = '0' WHERE email = '$userid'";
mysql_query($sql) or die(mysql_error());

echo $nb_result;

问题是,因为我是jquery / Js的新手,我不知道如何将结果放在div中。现在它停留在页面顶部,高于一切。 我尝试过,在数据函数内但没有工作......:

$('#test_app').html(data);

我想这是一个非常愚蠢的问题但是真的很感激帮助!非常感谢你。

更新:这里是HTML代码

 <?php 
  session_start();
 include ('connection/database.php');
 include ('login_script.php');
 include ('notifications.php');


 if($logged_in){
 ?>


 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Home</title>
</head>

<body>
<link rel="stylesheet" type="text/css" href="ms.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> 
<script src="js/font_i/specimen_files/easytabs.js" type="text/javascript" charset="utf-8"></script> 
<!-- FONT SCRIPT--> 
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#container').easyTabs({defaultContent:1});
    });
</script> 
 <script type="text/javascript" charset="utf-8">

var scrollSpeed = 70;       // Speed in milliseconds
var step = 1;               // How many pixels to move per step
var current = 0;            // The current pixel row
var imageHeight = 505;     // Background image height
var headerHeight = 59;     // How tall the header is.

  //The pixel row where to start a new loop
  var restartPosition = -(imageHeight - headerHeight);

 function scrollBg(){

//Go to next pixel row.
current -= step;

//If at the end of the image, then go to the top.
if (current == restartPosition){
    current = 0;
}

//Set the CSS of the header.
$('#main_logo').css("background-position","0 "+current+"px");


}

//Calls the scrolling function repeatedly
var init = setInterval("scrollBg()", scrollSpeed)  

</script> 

<!-- NOTIFICATIONS_SCRIPT !!!!!! -->
<script type="text/javascript" charset="utf-8">

setInterval(function() {

$.post('notifications.php', {

    email: 123

}, function(data) {


 });
 }, 30000);

 </script>


<style>
body{
font-family:SommetRoundedLight;
 }

 </style>
 </head>
 <body>
 <div id="container">
  <div id="header_contain">
   <div id="test_app">
   </div>
  </div>
 </div>
 </body>
 </html>

 <?php  }
 else{
       echo '<script language="javascript" type="text/javascript">
 <!--
 window.location.replace("connexion.php");
 -->
 </script>'; 

  } ?>

再次感谢

2 个答案:

答案 0 :(得分:0)

使用.append()代替.html()

答案 1 :(得分:0)

而不是

$('#test_app').html(data);

只需使用:

$('#test_app').html('<div>' + data + '</div>');

似乎很简单。我错过了什么吗?