我的网站上有一个shoutbox作为叠加层(它始终显示),您可以通过按下按钮(切换)来最小化和放大。
问题是,当我在我的网站上访问不同的页面时,最初的shoutbox始终是打开的。这很烦人。我想存储用户是否已最小化shoutbox的信息(例如在sql db中),以便shoutbox的状态不会总是返回。
任何帮助都将不胜感激!!
在我的shoutbox代码下面:
$("#menu_button").mouseenter(function () {
$("#menu").show();
});
$("#menu").mouseleave(function () {
$(this).hide();
});
答案 0 :(得分:1)
所以你要做的就是在PHP中使用cookies,例如试试这个:
shout.php(在返回内容之前的文件末尾)
setcookie("COOKIENAMEHERE", $output_message, time() + (86400 * 30), '/'); // 86400 = 1 day, therefore 30 days
然后要检索页面上的cookie,您可以在html页面中设置内容(请提供您的HTML / PHP,以便我可以根据您的场景进行调整):
<?php
$cookie_name = 'COOKIENAMEHERE';
if(isset($_COOKIE[$cookie_name])) { ?>
<div class="message_box">
<?php echo $_COOKIE[$cookie_name]; ?>
</div>
<?php } ?>
为了使喊话框默认隐藏,请将其放入css文件中:
.message_box {
display: none;
}
此外,如果您想要更新或删除cookie,您只需执行与更新和删除相同的操作:
$cookie_name = 'COOKIENAMEHERE';
unset($_COOKIE[$cookie_name]); //removes the cookie from memory
// empty value and expiration one hour before just to be sure
$result = setcookie($cookie_name, '', time() - 3600);
编辑:代码
...
elseif($_POST["fetch"]==1)
{
$results = mysqli_query($sql_con,"SELECT user, message, date_time FROM (select * from table ORDER BY id DESC LIMIT 10) table ORDER BY table.id ASC");
while($row = mysqli_fetch_array($results))
{
$output_message = $output_message.'<div class="shout_msg"><time>'.$row["date_time"].'</time><span class="username">'.$row["user"].'</span> <span class="message">'.$row["message"].'</span></div>';
}
setcookie("shout_msg", $output_message, time() + (86400 * 30), '/'); // 86400 = 1 day, therefore 30 days
echo $output_message;
}
else
{
header('HTTP/1.1 500 Are you kiddin me?');
exit();
}
}
在index.php文件中:
<div class="header">Shout Box <div class="close_btn"> </div></div>
<div class="toggle_chat">
<?php
$cookie_name = 'shout_msg';
if(isset($_COOKIE[$cookie_name])) { ?>
<div class="message_box">
<?php echo $_COOKIE[$cookie_name]; ?>
</div>
<?php } ?>
<div class="user_info">
<input name="shout_message" id="shout_message" type="text" placeholder="Type Message Hit Enter" maxlength="100" />
</div>
</div>
</div>
除非我读错了,否则你正在消失.message_box,请将此添加到你的CSS中:
.message_box {
display: none;
}
专业提示:将您的脚本放在关闭正文标记之前,以加快您的网站速度,将其置于头部使其加载速度变慢。
答案 1 :(得分:0)
这是带有shoutbox的页面的HTML / PHP;
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
.shout_box {
background: #000000;
width: 260px;
overflow: hidden;
position: fixed;
bottom: 0;
right: 10px;
z-index:9;
}
.shout_box .header .close_btn {
background: url(close_btn.gif) no-repeat 0px 0px;
float: right;
width: 15px;
height: 15px;
}
.shout_box .header .close_btn:hover {
background: url(close_btn.gif) no-repeat 0px -16px;
}
.shout_box .header .open_btn {
background: url(close_btn.gif) no-repeat 0px -32px;
float: right;
width: 15px;
height: 15px;
}
.shout_box .header .open_btn:hover {
background: url(close_btn.gif) no-repeat 0px -48px;
}
.shout_box .header{
padding: 5px 3px 5px 5px;
font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
font-weight: bold;
color:#fff;
border: 1px solid rgba(0, 39, 121, .76);
border-bottom:none;
cursor: pointer;
}
.shout_box .header:hover{
background-color: #000000;
}
.shout_box .message_box {
background: #FFFFFF;
height: 200px;
overflow:auto;
border: 1px solid #CCC;
}
.shout_msg{
margin-bottom: 10px;
display: block;
border-bottom: 1px solid #F3F3F3;
padding: 0px 5px 5px 5px;
font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
color:#7C7C7C;
}
.message_box:last-child {
border-bottom:none;
}
time{
font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
font-weight: normal;
float:right;
color: #D5D5D5;
}
.shout_msg .username{
margin-bottom: 10px;
margin-top: 10px;
}
.user_info input {
width: 98%;
height: 25px;
border: 1px solid #CCC;
border-top: none;
padding: 3px 0px 0px 3px;
font: 11px 'lucida grande', tahoma, verdana, arial, sans-serif;
}
.shout_msg .username{
font-weight: bold;
display: block;
}
-->
</style>
<script type="text/javascript" src="shoutbox/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// load messages every 1000 milliseconds from server.
load_data = {'fetch':1};
//window.setInterval(function(){
$.post('shoutbox/shout.php', load_data, function(data) {
$('.message_box').html(data);
var scrolltoh = $('.message_box')[0].scrollHeight;
$('.message_box').scrollTop(scrolltoh);
});
//}, 1000);
//method to trigger when user hits enter key
$("#shout_message").keypress(function(evt) {
if(evt.which == 13) {
var iusername = "java";
var imessage = $('#shout_message').val();
post_data = {'username':iusername, 'message':imessage};
//send data to "shout.php" using jQuery $.post()
$.post('shoutbox/shout.php', post_data, function(data) {
//append data into messagebox with jQuery fade effect!
$(data).hide().appendTo('.message_box').fadeIn();
//keep scrolled to bottom of chat!
var scrolltoh = $('.message_box')[0].scrollHeight;
$('.message_box').scrollTop(scrolltoh);
//reset value of message box
$('#shout_message').val('');
}).fail(function(err) {
//alert HTTP server error
alert(err.statusText);
});
}
});
//toggle hide/show shout box
$(".close_btn").click(function (e) {
//get CSS display state of .toggle_chat element
var toggleState = $('.toggle_chat').css('display');
//toggle show/hide chat box
$('.toggle_chat').slideToggle();
//use toggleState var to change close/open icon image
if(toggleState == 'block')
{
$(".header div").attr('class', 'open_btn');
}else{
$(".header div").attr('class', 'close_btn');
}
});
});
</script>
</head>
<body>
<div class="shout_box">
<div class="header">Shout Box <div class="close_btn"> </div></div>
<div class="toggle_chat">
<div class="message_box">
</div>
<div class="user_info">
<input name="shout_message" id="shout_message" type="text" placeholder="Type Message Hit Enter" maxlength="100" />
</div>
</div>
</div>
</body>
</html>
这是我的shout.php的php:
if($_POST)
{
//connect to mysql db
$sql_con = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
if(isset($_POST["message"]) && strlen($_POST["message"])>0)
{
//sanitize user name and message received from chat box
//You can replace username with registerd username, if only registered users are allowed.
$message = filter_var(trim($_POST["message"]),FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
$date = date("Y-m-d H:i");
$username = filter_var(trim($_POST["username"]),FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
//insert new message in db
if(mysqli_query($sql_con,"INSERT INTO table(user, message, date_time) value('$username','$message','$date')"))
{
//$msg_time = date('h:i Y-m-d',time()); // current time
$msg_time = date("Y-m-d H:i");
echo '<div class="shout_msg"><time>'.$msg_time.'</time><span class="username">'.$username.'</span><span class="message">'.$message.'</span></div>';
}
}
elseif($_POST["fetch"]==1)
{
$results = mysqli_query($sql_con,"SELECT user, message, date_time FROM (select * from table ORDER BY id DESC LIMIT 10) table ORDER BY table.id ASC");
while($row = mysqli_fetch_array($results))
{
echo '<div class="shout_msg"><time>'.$row["date_time"].'</time><span class="username">'.$row["user"].'</span> <span class="message">'.$row["message"].'</span></div>';
}
}
else
{
header('HTTP/1.1 500 Are you kiddin me?');
exit();
}
}