Drevin
12-22-2006, 08:08 AM
So here's what I'm trying to accomplish basically. I want to have an almost "live" chat, so I figured AJAX would be perfect for what I wanted.
So far, I've managed to get AJAX to retrieve any updates in the chatbox. That was pretty simple. My real problem however, is trying to get AJAX to send data over POST. It just won't work for me... and the frustration is killing me.
It just doesn't seem to even try the function I'm telling it to do. I press the button, and it attempts to load the page again... which I don't think it should?
No data is sent. Huh?
My page consists of 4 seperate pages. The index, getnew.php, which queries the database for the latest messages, addnew.php, which should be self-expanitory, and an HTML forms page.
Here is the index page, which consists of all my Javascript code. (Sorry ahead of time if it's hard to read... I usually make my code all pretty after I'm done... :P)
var url = "getnew.php";
var urlPost = "addnew.php";
var params = "";
http_request = getHTTP();
getChatData();
function getHTTP() {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return http_request;
}
function getChatData() {
http_request.onreadystatechange = chatData;
http_request.open('GET', url, true);
http_request.send(null);
setTimeout('getChatData()', 1500);
}
function sendChatData() {
http.open("POST", urlPost, true);
//Send the proper header information along with the request
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//http_request.setRequestHeader("Content-length", params.length);
//http_request.setRequestHeader("Connection", "close");
http_request.onreadystatechange = function() {//Call a function when the state changes.
if (http_request.readyState == 4 && http.status == 200) {
//chatText = encodeURIComponent(document.forms['chat_form'].elements['msg'].value);
//chatName = encodeURIComponent(document.forms['chat_form'].elements['name'].value);
params = "n=" + chatName + "&text=" + chatText;
alert(chatText + chatName);
}
}
http_request.send(params);
alert(http_request.reponseText);
/*
http_request.onreadystatechange = sendData;
param = "n=" + chatName + "&text=" + chatText;
http_request.open("POST", urlPost, true);
http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
http_request.send(param);
*/
}
function chatData() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById("chat_area").innerHTML = http_request.responseText;
}
}
}
My forms page
<form id="chat_form" name="chat_form" method="POST" action="">
Name: <input type="text" name="name" maxlength="20" id="name" size="21">
Message: <input type="text" name="msg" maxlength="100" id="msg" size="85">
<input type="submit" value="SayIt" id="submit" onclick="sendChatData()">
</form>
And my "addnew.php" page
<?
include("connect.php");
$name = $_POST['n'];
$msg = $_POST['text'];
if ( (!(isset($name))) || (!(isset($msg))) ) {
die("unset variables");
}
mysql_query("INSERT INTO ajax_tbl (msg_author, msg_text) VALUES ('$name', '$msg')") or die(mysql_error());
?>
My "getnew.php" isn't causing any troubles, so I won't post it unless needed.
Any ideas? :confused: (Sorry if I missed something important, just let me know)
So far, I've managed to get AJAX to retrieve any updates in the chatbox. That was pretty simple. My real problem however, is trying to get AJAX to send data over POST. It just won't work for me... and the frustration is killing me.
It just doesn't seem to even try the function I'm telling it to do. I press the button, and it attempts to load the page again... which I don't think it should?
No data is sent. Huh?
My page consists of 4 seperate pages. The index, getnew.php, which queries the database for the latest messages, addnew.php, which should be self-expanitory, and an HTML forms page.
Here is the index page, which consists of all my Javascript code. (Sorry ahead of time if it's hard to read... I usually make my code all pretty after I'm done... :P)
var url = "getnew.php";
var urlPost = "addnew.php";
var params = "";
http_request = getHTTP();
getChatData();
function getHTTP() {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
return http_request;
}
function getChatData() {
http_request.onreadystatechange = chatData;
http_request.open('GET', url, true);
http_request.send(null);
setTimeout('getChatData()', 1500);
}
function sendChatData() {
http.open("POST", urlPost, true);
//Send the proper header information along with the request
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//http_request.setRequestHeader("Content-length", params.length);
//http_request.setRequestHeader("Connection", "close");
http_request.onreadystatechange = function() {//Call a function when the state changes.
if (http_request.readyState == 4 && http.status == 200) {
//chatText = encodeURIComponent(document.forms['chat_form'].elements['msg'].value);
//chatName = encodeURIComponent(document.forms['chat_form'].elements['name'].value);
params = "n=" + chatName + "&text=" + chatText;
alert(chatText + chatName);
}
}
http_request.send(params);
alert(http_request.reponseText);
/*
http_request.onreadystatechange = sendData;
param = "n=" + chatName + "&text=" + chatText;
http_request.open("POST", urlPost, true);
http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
http_request.send(param);
*/
}
function chatData() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById("chat_area").innerHTML = http_request.responseText;
}
}
}
My forms page
<form id="chat_form" name="chat_form" method="POST" action="">
Name: <input type="text" name="name" maxlength="20" id="name" size="21">
Message: <input type="text" name="msg" maxlength="100" id="msg" size="85">
<input type="submit" value="SayIt" id="submit" onclick="sendChatData()">
</form>
And my "addnew.php" page
<?
include("connect.php");
$name = $_POST['n'];
$msg = $_POST['text'];
if ( (!(isset($name))) || (!(isset($msg))) ) {
die("unset variables");
}
mysql_query("INSERT INTO ajax_tbl (msg_author, msg_text) VALUES ('$name', '$msg')") or die(mysql_error());
?>
My "getnew.php" isn't causing any troubles, so I won't post it unless needed.
Any ideas? :confused: (Sorry if I missed something important, just let me know)