Log in

View Full Version : Simple JS Help I think [Nets :P HELP!]



Dentafrice1
18-04-2007, 11:38 PM
Ok,

I have an ajax code to read a .php file, and I have my alert PHP file.

This is for an alert system. But instead of a refreshing iframe, I want ajax to do it.

I have it to work, but when there isn't an alert (or the one your session is set to) it shows a blank alert.

Very confusing in my wording it.

Page > serverTime.php sets session to the id of the alert > shows alert
Page > serverTime.php reads session, if no new alert echo ""; > shows blank alert

I don't want it to show any alert if there is already a session

+REP for all help! :D

nets
18-04-2007, 11:48 PM
if(HttpRequest.responseText != '') {
alert(HttpRequest.responseText);
}
?

You may have named your HttpRequest object something else.

Dentafrice1
18-04-2007, 11:56 PM
<?
session_start();
$id = "401";
$message = "Caleb: test";
if($_SESSION[session_alert] != "$id") {
echo "$message";
$_SESSION[session_alert] = $id;
exit;
}
?>
Thats my servertime.php



<?php
session_start();
include("config.php");
?>
<script type="text/javascript">
var xmlHttp

function GetXmlHttpObject()
{
var objXMLHttp = null
if (window.XMLHttpRequest)
{
objXMLHttp = new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

function GetShouts()
{
xmlHttp = GetXmlHttpObject()
if (xmlHttp == null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="serverTime.php"
xmlHttp.open("GET", url, true)
xmlHttp.onreadystatechange = function ()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
if(xmlHttp.responseText != '') {
alert(xmlHttp.responseText);
}
;

}
}
};
xmlHttp.send(null);
}
GetShouts();
setInterval("GetShouts()", 1000);
</script>
<body onload="javascript:GetShouts();">
</body>
thats my ajax.php, somethings wrong..

nets
18-04-2007, 11:59 PM
You might have some whitespace in your serverTime.php page.


if(xmlHttp.responseText.replace(/^\s+|\s+$/g, '') != '') {
alert(xmlHttp.responseText);
}

Mentor
19-04-2007, 12:06 AM
You could add a null return?


<?
session_start();
$id = "401";
$message = "Caleb: test";
if($_SESSION[session_alert] != "$id") {
echo "$message";
$_SESSION[session_alert] = $id;
exit;
}else{
echo "!no!";
exit;
}
?>

So basicly instead of returning nothing it returns !no! when there is nothing, which is easyer to pick up on.

Then in your js just change it to

if(xmlHttp.responseText != '!no!') {
alert(xmlHttp.responseText);
}

o.0

Dentafrice1
19-04-2007, 12:08 AM
Thanks carl and nets ;)

+REP

Want to hide these adverts? Register an account for free!