PHP from within AJAX.....kindly help me out....

oracle

New Member
Messages
430
Reaction score
0
Points
0
Hello All,

I m trying to do the following:

index.php
which includes a chat.js file.

chat.js while recursively calls a js function using settimeout() function. The function in turns call a .php file, which do some connectivity with the database.

Now thing is how to pass a parameter from .php to this js function, so that i can appropriately take required steps. Here is a sample of .js and php function.

This is the javascript file, however i have saved it as .php because thr are some php codes too at the beginning:
-----------------------------------------------------------------------
function chat_session(url) {
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject){
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
}
}
}
if(!xmlhttp) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxx Take required steps xxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
intUpdate = setTimeout("chat_session('<?php echo $file ?>')", waittime)
}
}
xmlhttp.open('GET',url,true);
xmlhttp.send(null);
}

and here is the .php file which is being called from the above javascript function:
<?php
ob_start();
// set your infomation.
$dbhost='localhost';
$dbusername='xxxxx';
$dbuserpass='xxxxx';
$dbname='xxxxx';

// Connects to your Database
mysql_connect($dbhost,$dbusername,$dbuserpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

//checks cookies to make sure they are logged in
if(isset($_COOKIE['xxxxxxxx'])) {
$username = $_COOKIE['xxxxxxxxx'];
$pass = $_COOKIE['xxxxxxx'];
$check = mysql_query("SELECT * FROM USERS WHERE username = '$username'") or die(mysql_error());
while($info = mysql_fetch_array( $check )) {
//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password']) {
header("Location: http://localhost/login.php");
}
//otherwise they are shown the admin area
}
}
else {
//if the cookie does not exist, they are taken to the login screen
header("Location: http://localhost/login.php");
}

$validflag = "N";
$chat = mysql_query("select * from chat_sessions where receiver=\"$username\" and receiver_window=\"$validflag\"") or die(mysql_error());
while($inf = mysql_fetch_array($chat)) {
if($username == $inf['receiver']) {
// if thr is a window to be opened collect the sender info and open the window
$sender = $inf['sender'];
$receiver = $inf['receiver'];
$chatwindowflag = mysql_query("UPDATE chat_sessions SET receiver_window=\"Y\" where receiver=\"$receiver\"") or die(mysql_error());
}
}
ob_flush();
?>


Basically from .php file i just want to return, whether or not reciever_window flag being updated or not.

And the table which .php file is accessing is changing dynamically, hence i want the js funtion to invoke the php file continuously to check for some updation.

According to the required information i want to take some actions, like pop-up some window etc etc..


Hoping for your kind help.
 
Top