Hi,
i want a frame on my website to display one pic if the user has new messages and another different pic if it dsnt. Before i used html to auto refresh page every 10 secs.
that was annoying for the user. as the user could notice the refresh and the images used to flicker.
i found out that AJAX could perfrom the refresh for me better hence i tried using that.
but the frame dsnt refresh automatically, but it does display correct info when i refresh manually.
[PHP]<?php
session_start();
include "includes/db_connect.php";
include"includes/functions.php";
logincheck();
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM users WHERE username='$username'");
$fetch=mysql_fetch_object($query);
$inbox_msg=mysql_num_rows(mysql_query("SELECT * FROM `inbox` WHERE `read`='0' AND `to`='$username'"));
$check = mysql_query("SELECT `to`,`read` FROM `inbox` WHERE `read`='0' AND `to`='$username'");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
}
return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
function sendRequest(page) {
// Open PHP script for requests
http.open('get', page);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
// Text returned FROM the PHP script
var response = http.responseText;
if(response) {
// UPDATE ajaxTest content
document.getElementById("msgstatus").innerHTML = response;
}
}
}
function repeatloop()
{
sendRequest('box.php'); // replace "inbox-status.php" with your php page's url
setTimeout("repeatloop()", 3000);
}
window.onload=function() {
repeatloop();
}
</script>
<title>The Legendary Mafia</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-image: url(images/banner/gradient.gif);
background-repeat: repeat-x;
}
-->
</style></head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" height="70" background="" scope="col">
<div align="center"><p>
<?php
$inbox=mysql_num_rows($check);
if ($inbox > 0){
echo "<a href=inbox.php target=middle><img border=0 src=images/unread1.gif align=center width=16 height=11></a> ";
}else{
echo "<img src=images/read.gif align=center width=16 height=11>";
}
?>
<span id="msgstatus"></span></p>
</div></td>
</tr>
</table>
</body>
</html>[/PHP]
35 3357 gits 5,390
Expert Mod 4TB
could you have a look at the FF javascript console? is there any error?
could you have a look at the FF javascript console? is there any error?
i looked at the error console in FireFox and got the following errors: Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1//game/box.php :: handleResponse :: line 42" data: no]
Source File: http://127.0.0.1//game/box.php
Line: 42
Error: uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: http://127.0.0.1//game/box.php :: sendRequest :: line 36" data: no]
does this help?
Since you're reusing the first request and it's busy, it causes this error in Firefox. Make a new request or abort the first one. See this link for an explanation.
i am really not familiar with AJAX, i read the article but i find it a bit confusing. Could you possibly edit my ajax script for me?
Since you won't want to create new requests each time, try using abort() in your sendRequest function, e.g. something like: - if (http.readyState != 4) {
-
http.onreadystatechange = function () {}
-
http.abort();
-
}
I changed what you said, the errors no longer appear in the error console but it dsnt seem to refresh the icons as i should. However when i manually refresh the frame the correct icon is displayed.
please take a look at the code and tell me if i have done what you correctly:
[PHP]<?php
session_start();
include "includes/db_connect.php";
include"includes/functions.php";
logincheck();
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM users WHERE username='$username'");
$fetch=mysql_fetch_object($query);
$inbox_msg=mysql_num_rows(mysql_query("SELECT * FROM `inbox` WHERE `read`='0' AND `to`='$username'"));
$check = mysql_query("SELECT `to`,`read` FROM `inbox` WHERE `read`='0' AND `to`='$username'");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
}
return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
function sendRequest(page) {
// Open PHP script for requests
if (http.readyState != 4) {
http.onreadystatechange = function () {}
http.abort();
}
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
// Text returned FROM the PHP script
var response = http.responseText;
if(response) {
// UPDATE ajaxTest content
document.getElementById("msgstatus").innerHTML = response;
}
}
}
function repeatloop()
{
sendRequest('box.php'); // replace "inbox-status.php" with your php page's url
setTimeout("repeatloop()", 3000);
}
window.onload=function() {
repeatloop();
}
</script>
<title>The Legendary Mafia</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-image: url(images/banner/gradient.gif);
background-repeat: repeat-x;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" height="70" background="" scope="col">
<div align="center"><p>
<?php
$inbox=mysql_num_rows($check);
if ($inbox > 0){
echo "<a href=inbox.php target=middle><img border=0 src=images/unread1.gif align=center width=16 height=11></a> ";
}else{
echo "<img src=images/read.gif align=center width=16 height=11>";
}
?>
<span id="msgstatus"></span></p>
</div></td>
</tr>
</table>
</body>
</html>[/PHP]
You weren't supposed to replace sendRequest(), but add to it, e.g. - function sendRequest(page) {
-
if (http.readyState != 4) {
-
http.onreadystatechange = function () {}
-
http.abort();
-
}
-
// Open PHP script for requests
-
http.open('get', page);
-
http.onreadystatechange = handleResponse;
-
http.send(null);
-
}
i did what you said and i get the following error now, i am getting lots of these errors in the error console: Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1//game/box.php :: handleResponse :: line 49" data: no]
Source File: http://127.0.0.1//game/box.php
Line: 49
hmm, what could be wrong now? the edited ajax script is below: - <script language="javascript">
-
function createRequestObject() {
-
-
var req;
-
-
if(window.XMLHttpRequest){
-
// Firefox, Safari, Opera...
-
req = new XMLHttpRequest();
-
} else if(window.ActiveXObject) {
-
// Internet Explorer 5+
-
req = new ActiveXObject("Microsoft.XMLHTTP");
-
} else {
-
// There is an error creating the object,
-
// just as an old browser is being used.
-
alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
-
}
-
-
return req;
-
-
}
-
-
// Make the XMLHttpRequest object
-
var http = createRequestObject();
-
-
-
function sendRequest(page) {
-
if (http.readyState != 4) {
-
http.onreadystatechange = function () {}
-
http.abort();
-
-
}
-
-
// Open PHP script for requests
-
http.open('get', page);
-
http.onreadystatechange = handleResponse;
-
http.send(null);
-
-
}
-
-
-
function handleResponse() {
-
-
if(http.readyState == 4 && http.status == 200){
-
-
// Text returned FROM the PHP script
-
var response = http.responseText;
-
-
if(response) {
-
// UPDATE ajaxTest content
-
document.getElementById("msgstatus").innerHTML = response;
-
}
-
-
}
-
-
}
-
-
function repeatloop()
-
{
-
sendRequest('box.php'); // replace "inbox-status.php" with your php page's url
-
setTimeout("repeatloop()", 3000);
-
}
-
-
window.onload=function() {
-
repeatloop();
-
}
-
</script>
i just checked the errors again and found a second error, this one is displayed less frequently in the error list than the first error: Error: http is not defined
Source File: http://127.0.0.1//game/box.php
Line: 49
Line 49 seems to be this line: - if(http.readyState == 4 && http.status == 200){
Use a try..catch block when checking for the status: - if (http.readyState == 4) {
-
try {
-
if (http.status == 200) ...
-
...
-
} catch ...
what about fixing the other error? that is the main error which i get every time the ajax script is meant to refresh:
Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1//game/box.php :: handleResponse :: line 49" data: no]
Source File: http://127.0.0.1//game/box.php
Line: 49
how do i implement your catch changes? i dnt get it
I think it's time somebody just deleted all of their code and re-wrote it... But that's just me.
gits 5,390
Expert Mod 4TB
hi ...
first remove the useless lines 27 - 31 of your code and try again ... please. or in case you want to abort a running request we have to adapt that code ... see my next posting :)
kind regards
gits 5,390
Expert Mod 4TB - function sendRequest(page) {
-
if (typeof window.http != 'undefined' && window.http.readyState != 4) {
-
http.abort();
-
}
-
-
window.http = createRequestObject();
-
-
http.open('get', page);
-
http.onreadystatechange = handleResponse;
-
http.send(null);
-
}
kind regards
ps: btw ... are 3 sec enough? with the shown code the request will be aborted when it isn't ready yet and a new one is started ... so try a longer refresh time ...
thanks for your help, now my script is like: - <script language="javascript">
-
function createRequestObject() {
-
-
var req;
-
-
if(window.XMLHttpRequest){
-
// Firefox, Safari, Opera...
-
req = new XMLHttpRequest();
-
} else if(window.ActiveXObject) {
-
// Internet Explorer 5+
-
req = new ActiveXObject("Microsoft.XMLHTTP");
-
} else {
-
// There is an error creating the object,
-
// just as an old browser is being used.
-
alert("Your Browser Does Not Support This Script - Please Upgrade Your Browser ASAP");
-
}
-
-
return req;
-
-
}
-
-
// Make the XMLHttpRequest object
-
var http = createRequestObject();
-
-
-
-
function sendRequest(page) {
-
if (typeof window.http != 'undefined' && window.http.readyState != 4) {
-
http.abort();
-
}
-
-
window.http = createRequestObject();
-
http.open('get', page);
-
http.onreadystatechange = handleResponse;
-
http.send(null);
-
}
-
-
-
function handleResponse() {
-
-
if(http.readyState == 4 && http.status == 200){
-
-
// Text returned FROM the PHP script
-
var response = http.responseText;
-
-
if(response) {
-
// UPDATE ajaxTest content
-
document.getElementById("msgstatus").innerHTML = response;
-
}
-
-
}
-
-
}
-
-
function repeatloop()
-
{
-
sendRequest('box.php'); // replace "inbox-status.php" with your php page's url
-
setTimeout("repeatloop()", 9000);
-
}
-
-
window.onload=function() {
-
repeatloop();
-
}
-
</script>
but i get the following errors:
Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://127.0.0.1//game/box.php :: handleResponse :: line 47" data: no]
Source File: http://127.0.0.1//game/box.php
Line: 47
Error: http is not defined
Source File: http://127.0.0.1//game/box.php
Line: 47
to be honest i am getting really annoyed with this ajax script. I didn't write it myself, i took it from a website. I would completely re-write it if i knew how to code in javascript. Is it worth fixing this script or do you have any other fully working ajax refresh scripts?
please let me know
thanks
gits 5,390
Expert Mod 4TB - function createRequestObject() {
-
var req;
-
-
if(window.XMLHttpRequest){
-
req = new XMLHttpRequest();
-
} else if(window.ActiveXObject) {
-
req = new ActiveXObject("Microsoft.XMLHTTP");
-
} else {
-
alert("Your Browser Does Not Support This Script");
-
}
-
-
return req;
-
}
-
-
function sendRequest(page) {
-
if (typeof window.http != 'undefined' && window.http.readyState != 4) {
-
window.http.abort();
-
}
-
-
window.http = createRequestObject();
-
window.http.open('get', page);
-
window.http.onreadystatechange = handleResponse;
-
window.http.send(null);
-
}
-
-
function handleResponse() {
-
if (window.http.readyState == 4) {
-
var response = window.http.responseText;
-
-
if (response) {
-
document.getElementById('results').value = response;
-
}
-
}
-
}
-
-
function repeatloop() {
-
sendRequest('test.txt');
-
setTimeout(repeatloop, 1000);
-
}
-
-
window.onload=function() {
-
repeatloop();
-
}
-
with a local page the shown code worked ... note i changed the page to 'test.txt' and i leaved the http.status condition out ... give it a try ... it SHOULD work, and i used a textarea with id='results'
kind regards
i used the new script you wrote. The error console doesn't pick up any errors.
However on my website whenever i try to open any page i get the following error: Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\game\includes\functions.php on line 26
the file name and line number change according to whatever page i am trying to access :(
gits 5,390
Expert Mod 4TB
i assume you changed 'test.txt' back to 'box.php' and you replaced the code in your script-tags with the given one? please show the code again that your are using now ... the code i showed you MUST work ...
kind regards
here is the full code now, when a page takes longer than 60 seconds to load or even if the include file take a long time to execute i get the timeout error. the full code is below:
[PHP]<?php
session_start();
include "includes/db_connect.php";
include"includes/functions.php";
logincheck();
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM users WHERE username='$username'");
$fetch=mysql_fetch_object($query);
$inbox_msg=mysql_num_rows(mysql_query("SELECT * FROM `inbox` WHERE `read`='0' AND `to`='$username'"));
$check = mysql_query("SELECT `to`,`read` FROM `inbox` WHERE `read`='0' AND `to`='$username'");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Your Browser Does Not Support This Script");
}
return req;
}
function sendRequest(page) {
if (typeof window.http != 'undefined' && window.http.readyState != 4) {
window.http.abort();
}
window.http = createRequestObject();
window.http.open('get', page);
window.http.onreadystatechange = handleResponse;
window.http.send(null);
}
function handleResponse() {
if (window.http.readyState == 4) {
var response = window.http.responseText;
if (response) {
document.getElementById('results').value = response;
}
}
}
function repeatloop() {
sendRequest('box.php');
setTimeout(repeatloop, 5000);
}
window.onload=function() {
repeatloop();
}
</script>
<title>The Legendary Mafia</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body {
background-image: url(images/banner/gradient.gif);
background-repeat: repeat-x;
}
-->
</style>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="100%" height="70" background="" scope="col">
<div align="center"><p>
<?php
$inbox=mysql_num_rows($check);
if ($inbox > 0){
echo "<a href=inbox.php target=middle><img border=0 src=images/unread1.gif align=center width=16 height=11></a> ";
}else{
echo "<img src=images/read.gif align=center width=16 height=11>";
}
?>
<span id="results"></span></p>
</div></td>
</tr>
</table>
</body>
</html>[/PHP]
I am getting the following error now in the error console: Error: document.getElementById("results") has no properties
Source File: http://127.0.0.1//game/box.php
Line: 38
i also get this error in the console: Error: window.http has no properties
Source File: http://127.0.0.1//game/box.php
Line: 34
I am getting the following error now in the error console: Error: document.getElementById("results") has no properties
Source File: http://127.0.0.1//game/box.php
Line: 38
A span doesn't have a value property. Use innerHTML instead .
A span doesn't have a value property. Use innerHTML instead .
whats an inner html?
gits 5,390
Expert Mod 4TB
so when you alert the response you have the correct data?
gits 5,390
Expert Mod 4TB
use: - document.getElementById('results').innerHTML = response;
i am getting the following problems: 1) error in the console: Error: document.getElementById("results") has no properties
Source File: http://127.0.0.1//game/box.php
Line: 38 2) if user get a new message the frame refreshes and shows the "new message" pic under the original "no message" pic. If i scroll down the frame i see a whole column of "new message" images.
here is a pic to explain: 3) similarly if i read the new message the frame updates and shows a "no message" pic underneath the "new message" pic. If i scroll down the frame i see a whole column of "no message" images.
here is a pic to explain:
i feel we are very close to a solution. :)
gits 5,390
Expert Mod 4TB
hi ... what do you mean with the frame refreshes? is the entire frame to be refreshed? i'm wondering about the getElementById('results')-error because the span is in that page ... but there is certainly a problem, when the ajax-request is started and meanwhile the page refreshes ... if that would be the case ... then we found the problem ...
kind regards
the frame consists of the page named "box.php". The AJAX script refreshes that page not the frame.
I dnt think frames can be refreshed only the page within that frame.
So basically the page refreshes not the frame.
What do you assume the problem is?
gits 5,390
Expert Mod 4TB
of course ... i assumed that when i say that the frame refreshes the page is reloaded ... so is the ajax call replacing the entire document there, or only a particular part of the page? in case it is then the document is reloaded and the dom may not be ready to use ... so the span couldn't be found ... so what is the response of one ajax-call?
well you have the page infront of you. the page consists of two things:
1) AJAX auto refresh script
2) the small PHP script
so i think the ajax refreshes the whole page which would include the php script.
gits 5,390
Expert Mod 4TB
so we are at the beginning again ;) ... when the request is started it requests the box.php page ... that returns en entire html-page? do you use firefox and firebug? you could see what the request.response is in firebug ... could you post it here or could you give a link to a testpage ... please? we cannot put a html-document into a node ...
kind regards
i have installed firebug. Now how do i use it?
also is there no other ajax refresh script which works? all i want is the script to refresh the image if the user has new messages. why is this proving so hard to do?
:(
i am enclosing a screenshot showing the response times of the script. the create request apparently is not working. http://img340.imageshack.us/img340/3624/errormb8.png gits 5,390
Expert Mod 4TB
there is no 'ajax-refresh-script' ... the simplest way would be to call another php-script that returns you whether the user has new messages or not ... a simple true or false in the responseText ... and in case it is true you simply switch the src-attribute of the img to the required src-file. there are a lot of GET-requests in the screenshot ... open one and have a look at the response ...
kind regards
so gits i should seprate the php file from the ajax script?
should i put the AJAX script in one .php file and ask the frame to show that file in the frame?
then i should also put the php script in a seperate file and tell the ajax script to call that?
or how else shoudl i do it? I think maybe the problem arises from the fact that both files are in the same file?
gits 5,390
Expert Mod 4TB
it seem so ... :) typically you should have a php-file that makes a query and sends its result back to the browser that invoked it through an ajax-call ... so the request should simply ask a 'datasource' for new data ... in your case the query simply needs to return true or false for the new messages ... or even better the the number of new messages ... so you could update your span with the new number of messages and a 'new messages' image in case the number is > 0 ... otherwise leave it ;)
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics
by: simon |
last post by:
hello,
i have a section of a page that has multiple dropdowns.
each dropdown calls the same method on a change of selection by the
user. the method loops over all the dropdowns and if the newly...
|
by: KatMagic |
last post by:
Is there a way to automatically refresh a user control? I have a page where I automatically refresh the page with <meta http-equiv="refresh" content="600">
The page loads a user control based on...
|
by: ak1dnar |
last post by:
Hi,
I am developing a web site that displays Products, and along with each products there is a Button.There is a <DIV> in my page to display Number of Products Added to the Basket.
Items : 0
...
|
by: Piotr Nowak |
last post by:
Hi,
Say i have a server process which listens for some changes in database.
When a change occurs i want to refresh my page in browser by notyfinig it.
I do not want to refresh my page i.e....
|
by: mitchell |
last post by:
hi
i m using IE 6.0.
i want to refresh just the captcha part when the user clicks on an image.
i searched for articles on it and got a fairly good understanding of it.
but i m still unable to...
|
by: Kaante |
last post by:
Hi,
I basically have two frames on my page, the top one contains users stats and the bottom frame contains the website.
I want to have a message icon on the top frame which would flash once the...
|
by: malcster2 |
last post by:
hello, i am a beginner to ajax.
i have created a mysql database, which i would like to access from a web page.
i have created 3 files, a html to display the data, a php file to extract the data,...
|
by: Peter |
last post by:
I have a APS.NET 3.5 webpage which calls a web service.
What I need is to update this page automatically when a value changes in the
webservice, does anyone have an example?
Thank You
...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |