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(开发者_C百科);
}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>
Why not just set dataType to "script" with your $.ajax call-- and have the php script that the $.ajax method calls generate the appropriate javascript to insert/remove/show/hide/modify items on your page accordingly?
I think you're over-thinking this problem, use of sessions in this case i think is unnecessary as well... and I would if at all possible try to avoid that.
Look here-- http://api.jquery.com/jQuery.ajax/ -- at the parameter "dataType" for the specific parameter "script" and I think you'll find it helpful in what you're trying to accomplish.
There is no needs for sessions unless you want the message to stay across multiple pages.
If your trying to send the user to another page after pressing submit store the ajax response into a hidden input and use $_POST to retrieve it.
If same page, just save the ajax response to box and call box.show() to display.
Good?
I think that you'll need comet to get rid of the requests. However, I haven't used it myself and I don't know much about it so I can't really help you with that.
精彩评论