i've create a chat in ajax, but i still have some problem that i can't recognize. When the page is loaded, the php load all the opened chat and relative mex, then there's an addmex ajax function: when the user hits enter it's called passing this.value and the cod_chat. The ajax function send these data to the php, which insert them into the db and add the last mex into the chat. Every X seconds an update function is called, to update the chat. It all works until it gets to the addmex function: the php works, because it add the mex into the db, but for some reason it refresh the page. Could i use RTMP?
This is the code of the form:
<input type='text' class='chat_input' value='write something' onkeydown='if (event.keyCode == 13){addmex(this.value); this.value='';}'/>
and this is the code of the ajax function (don't worry about the php variables because this function is loaded by echo):
function addmex(mex)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","addmex.php?cod_contatto="+' . $cod_contatto . '+"&mex="+mex,false);
xmlhttp.send();
document.getElementById("messaggi_' . $cod_contatto . '").innerHTML+=xmlhttp.responseText;
}
I set Async as false because it respond slowly, and if i set true works only with some alert that let the server part complete. Any idea?
How is your insert field implemented? If it is in a HTML form, it will send the form when you hit enter. So if this is given, you need to prevent the default enter action in order to prevent the page refresh: Just get the js event (onkeydown), do what you need to do and return false.
EDIT
You may assign the handler in a diffrent way: Give the input an ID so this will work:
document.getElementById(yourID).onkeydown = function(event) {
if(event.keyCode == 13){addmex(this.value); this.value='';}
return false;
}
EDIT 2
Your first code has a syntax error, it should be:
.... this.value="";}'/></form>
You cant use the same quotes in the inner String: '''' are like 2 strings, while '""' is one string in another.
SOLUTION For those who happen to read this.
The form tag was unnecessary and caused the page to refresh (sometimes preventing default does not work?!).
Finally there was the refreshing via ajax missing. Just do a setInterval() with the function and it should work all fine.
Maybe you can try ArcusNode or Cumulus. I suggest ArcusNode to proceed with. It's an OpenRTMFP protocol.
I've made a mini project script with mysql and this works realy perfect ! You dont need HTTPS for to hidding ajax file. Try this. THIS 100 % WORKS !
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `ajax_sessions`;
CREATE TABLE `ajax_sessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sid` text NOT NULL,
`sip` varchar(18) NOT NULL,
`open` int(1) NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
// INCLUDE YOUR MYSQL CONNECT FILE
require '/engine/config/mysql.php';
// SESSION IP ADDRESS
$sip = $_SERVER['REMOTE_ADDR'];
// COUNT IF TABLE FOR THIS IP ADDRESS EXISTS
$count = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM ajax_sessions WHERE sip = '".$sip."'"));
// COUNT VAR
$count = $count['COUNT(*)'];
// GENERATE RANDOM SESSION ID
$sid = substr(str_shuffle("_-0123456789-_-abcdefghijklmnopqrstuvwxyz-_-ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"), 0, 50);
// YOUR AJAX / XMLHTTP / (LOAD) CALL
echo '
<script>
$("button").click(function() {
$(".container").load("/ajax/request.php?sid='.$sid.'");
});
// CHEC IF CODE WORKS
console.log("Ajax - Session control is activated.");
</script>';
// IF TABLE FOR THIS IP ADDRESS NOT EXISTS INSERT IP,SESSION AND OPEN SESSION.
if($count<1) {mysql_query("INSERT INTO ajax_sessions (sid,sip,open) VALUES ('".$sid."','".$sip."','1')");
} else {
// IF THIS IP ALREADY EXISTS INTO TABLE JUST UPDATE IT AND INSERT SESSION ID.
mysql_query("UPDATE ajax_sessions SET sid = '".$sid."', sip = '".$sip."', open = '1'");
}
// IF GET sid ( SESSION ID )
if($_GET) {
// CHECK IF SESSION ID IS SET
if(isset($_GET['sid']) && $_GET['sid'] == $_GET['sid'] && !empty($_GET['sid'])) {
// SESSION REMOTE IP ADDRESS
$sip = $_SERVER['REMOTE_ADDR'];
// GET SESSION ID WE GOT
$sid = $_GET['sid'];
// COUNT IF THIS SESSION EXISTS IN MYSQL BASE
$fetch = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM ajax_sessions WHERE sid = '".$sid."' AND sip = '".$sip."' AND open = '1'"));
// COUNT SESSION
if($fetch['COUNT(*)'] == 1) {
// IF SESSION EXISTS CATCH IP ADDRESS AND CLEAR SESSION ID FOR THIS IP AND CLOSE IT, BUT NOT REMOVE
mysql_query("UPDATE ajax_sessions SET sid = NULL, sip = '".$sip."', open = '0'");
} else {
// DIE IF SESSION NOT EXISTS AND DO NOT SHOW CODE
die;
}
// IF IT WORKS OPEN JS CONSOLE LOG WITH F12 FOR GOOGLE CHROME
echo '<script>console.log("Session unseted !")</script>';
}
// AJAX POST CONTENT. IF POST
} else if($_POST) {
echo 'Call request';
}