Jump to content


Photo

reloading a page without refresh


  • Please log in to reply
8 replies to this topic

#1 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 27 September 2006 - 07:11 AM

ok, i am needing to know of a way to reload a page without having to refresh, what i have is a instant messenger that sends a request to a user, if the user is browseing my site and they recieve a request while looking at a page it will show up on the page that they have a incoming request, right now the way i have it is that the page has to be refreshed or the user has to go to another page before it displays that they have a request.

All help appreciated.

BTW, I have looked into javascript but have NEVER used or fooled around with javascript so i have absolutely no idea on what i am doing.

#2 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 September 2006 - 07:13 AM

Would think you are going to need to get the hang of javascript for this, sounds like a job for ajax

#3 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 27 September 2006 - 07:15 AM

see that's what i figured, i tried messing with it a little bit but it just seems to confusing, i would really appreciate like a point in the right direction towards this matter, since this is basicly the only thing i will need javascript for in my project.

#4 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 September 2006 - 07:21 AM

Have a look at the ajax forum: http://www.phpfreaks...board,51.0.html

#5 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 27 September 2006 - 07:30 AM

I have gone there haven't found anything of any use, i went over to ajaxfreaks.com and browsed through there but still nothing, i have googled on it now for about 3 days and am getting no where.

#6 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 September 2006 - 08:11 AM

Well not sure 100% the best way of doing this. Perhaps have a javascript timer that will activate a link for a transparent frame with a url for the user id in handling script or use a $_SESSION user id for a more secure system, this will direct to a script that will collect the get and query the database to see if a message has been set in the database if it has it will pull the information and update the messenger again using javascript.

I think either way this will be about 80% javascript and 20% php.



#7 skatermike21988

skatermike21988
  • Members
  • PipPipPip
  • Advanced Member
  • 100 posts
  • LocationFlorida

Posted 27 September 2006 - 08:20 AM

any ideas on how i would do it, like i said i have never fooled around with javascript

#8 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 27 September 2006 - 09:02 AM

I am going to have something similar in a live support app that I am developing.

Here is what I am going to do. This is more like what SMF does to inform the users of a new message.

The javascript

//I am not going to pretend that I completely understand this but it works
//this creates the request object
//for more informaton google XMLHttpRequest
function createRequestObject()
{
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
           xmlhttp.overrideMimeType('text/xml');
        }
        else if (window.ActiveXObject)
	{ // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
	return xmlhttp;
}

//here we define global variables for the following functions
var http = createRequestObject();
var mTimer;

//this function will be called when the page loads.
function pageLoad() {
     checkMessages();
}

//how we redirect the user if they have messages
function redirector() {
     window.location = 'http://www.yoursite.com/yourInbox.php';
}

function checkMessages() {
     //we open the file for processing with the get method
     http.open('get', 'yourpage.php?action=checkMessages');
     http.setRequestHeader('Content-Type', 'application/x-www-urlencoded');
     //if the page was opened successfuly we move on to the next function
     http.onreadystatechange = handleRequest();
     //this is the only real difference between the get and post method.
     //if we were posting something we would do something like this
     //http.send('var='+yourvar);
     http.send(null);
}

function handleRequest() {
     if (http.readystate == 4) {
         //the response from the php backend file
         var response = http.responseText;
         if (response == '') {
             //if the response is empty we set a timer to recheck for messages every 5 seconds
             mTimer = setTimeout('checkMessages();', 5000);
          } else {
             //we clear the timer
             clearInterval(mTimer);
             //confirm is alot like alert except that you can choose two options
             var agree = confirm(response);
             if (agree) {
                //if the user has messages and agrees to be redirected to the inbox
                setTimeout('redirector()', 5000);
                //this is realy unnecssicary. I only put this here so you would see the difference between alert and confirm
                alert('You will be redirected to your inbox/whatever in 5 seconds');
             }
       }
}

Now in the body of your html place this

<body onload="javascript:pageLoad();">


Now you can do something like this in the php script. NOTE you will have to change the location of the file in the getMessages function and the redirect in the handleRequest function.

<?php
session_start();
//this is going to assume that the user is logged in. 

$vaild_actions = array('checkMessages', 'something else');
$action = strip_tags($_GET['action']);
if (!in_array($action, $valid_actions)) {
    die("invaild action");
}

if ($action == "checkMessages") {
   if (!$_SESSION['username']) {
       echo "";
       exit();
   }
   $sql = sprintf("SELECT COUNT(*) AS `message_check` FROM `table` WHERE `username` = '%s'", $_SESSION['username']);
   $res = mysql_query($sql);
   $message_check = mysql_result($res, 0, 'message_check');
   
   if ($message_check == 0) {
       echo "";
   }else{
      echo "You have $message_check new messages. Click ok to view your inbox/whatever";
   }
}
?>
     
hope this helps,
Tom       



Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#9 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 27 September 2006 - 09:53 AM

I just thought of something..

You may run into an issue. This being that if the user chooses not to be redirected it will still check for new messages and will pop up every 5 seconds..lol

You may want to add something to the message table.. like popup or whatever.

Now when you add the message to the db set that field to accept. and if they deny the pop up do something like this..

first change this

$sql  = sprintf("SELECT COUNT(*) AS `message_check` FROM `table` WHERE `username` = '%s'", $_SESSION['username']);

To

$popup = "accept";
$sql = sprintf("SELECT COUNT(*) AS `message_check` FROM `table` WHERE `username` = '%s' AND `popup` = '%s'", $_SESSION['username'], $popup);

And now add this function to the javascript..

[code=php:0]
var deny = createRequestObject();
function denyPopup() {
     deny.open('get', 'yourpage.php?action=denyPopup');
     deny.setRequestHeader('Content-Type', 'application/x-www-urlencoded');
     deny.onreadystatechange = handleDeny();
     deny.send(null);
}

function handleDeny() {
    if (deny.readystate == 4) {
       var response = deny.responseText;
       alert(response);
    }
}
[/code]

and now you can add this to the php backend file

[code=php:0]
if ($action == "denyPopup") {
   $username = $_SESSION['username'];
   $sql = "UPDATE `table` SET `popup` = 'deny' WHERE `username` = '$username'";
   $res = mysql_query($sql);
   if (!$res) {
       echo "There was an error";
   }else{
       echo "Your messages are in your inbox when you are ready";
   }
}[/code]

You will also need to add the denyPopup to the vaild actions array.

also, chage the handleRequest function to this.

[code=php:0]
function handleRequest() {
     if (http.readystate == 4) {
         //the response from the php backend file
         var response = http.responseText;
         if (response == '') {
             //if the response is empty we set a timer to recheck for messages every 5 seconds
             mTimer = setTimeout('checkMessages();', 5000);
          } else {
             //we clear the timer
             clearInterval(mTimer);
             //confirm is alot like alert except that you can choose two options
             var agree = confirm(response);
             if (agree) {
                //if the user has messages and agrees to be redirected to the inbox
                setTimeout('redirector()', 5000);
                //this is realy unnecssicary. I only put this here so you would see the difference between alert and confirm
                alert('You will be redirected to your inbox/whatever in 5 seconds');
             } else {
                denyPopup();
             }
       }
}

That should fix the bug..

Good luck and Enjoy,
Tom


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users