Hello.
I have standed out for awhile now with a thing i want to do. Thing i want to do is to display a message(confirmation, "You have earned 2 points") at the top (message like stackoverflow).
Ive did this storing a confirmation message using session variable, and then at index.php there is a ajax script that loads session.php each 1 second, for checking if there's some message to display.
BUT i wonder, cant this be done in another way? I mean, does it need to check each 1 second all the time
My site is a design on index.php, and then you are browsing the rest inside a frame.
Anyway, i already got a solution for this as you see, i think it may request too much to the server.
So i wonder, cant this be done in another way? I mean, does it need to check each 1 second all the time?
Isnt it possible to, if you have inserted a comment correctly, and stored session variable called user_message, then tell index.php to show the #box.
My comment "form" is on show.php, which just contains a normal text area and a submit button. Now it stores the message through a JS script that passes it all to a string, to insert.php which inserts with normal mysql query to the database, and stores a session variable. (source code below)
Here's some coding: session.php:
<?php
session_start();
if(isset($_SESSION['user_message'])) {
echo 1;
}
?>
div box in index.php:
<div id='box' onclick="closeNotice()" style="display: none">
Hey, <b><? echo $pusername; ?></b> - <? echo $_SESSION["user_message"]; ?>
<a href="#" class="close-notify" onclick="closeNotice()">X</a>
</div>
<?php
$_SESSION["user_message"] = null;
?>
ajax script that refresh each 1 second(that i am using now):
function checkSession(){
$.ajax({url: "session.php", success: function(data){
if( data == 1){
$('#box').show();
}else{
$('#box').hide();
}
}});
}
setInterval('checkSession()',1000);
Script that passes string to insert.php which inserts the comment to the database:
var nocache = 0;
function insert() {
document.getElementById('insert_response').innerHTML = "Please Wait .. "
var fID= encodeURI(document.getElementById('fID').value);
var kommentar= encodeURI(document.getElementById('kommentar').value);
nocache = Math.random();
http.open('get', 'insert.php?fID='+fID+'&kommentar=' +kommentar+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
document.getElementById('insert_response').innerHTML = ''+response;
}
}
The form on show.php:
<div id="insert_response">
</div>
<form action="javascript:insert()" method="post">
<textarea id="kommentar" name="kommentar"></textarea><br />
<input type="hidden" name="fID" id="fID" value="<? echo $_GET["id"]; ?>" />
<input type="submit" name="Submit" value="Sæt ind!"/>
</form>
Alright i hope i didnt forget something to mention.
Please comment if you want some more information about something, or code to something.
Please dont make a answer, to complain if i missed out to tell you something, so comment and i will update my question.
Thank you and i hope i can solve this with a good method.