For some reason i just cannot get this to work, the user enters a comment and it sends it to the database - I was getting so stressed that I just made the script send a straight forward line to the database -
Code:
var params = "comm=hello&subject=binny";
rather than what's in the text box, so ignore that!
Here's what i have so far
Code:
<script>
function newComment()
{
var url = "leavecomment.php";
var params = "comm=hello&subject=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
}
</script>
</head>
<body>
<div>
<form name="test" >
<textarea id="comment" cols="30" rows="8"></textarea>
<input type="button" onClick="newComment()" value="Test"/>
</form>
1) Where is http defined? You'll have to show the code for it (also what browser are you testing in)
2) You are setting two headers I don't normally see. Content-length and Connection. Any particular reason?
Other than that at first glance it looks about right.
Hey sorry, extremely stupic mistake, I defined http and now it works fine
The only thing is, It wont give me a response, this is just a call to another script where it echos all the information from the database, everything gets written to the datbase but nothing gets returned:
Code:
function createRequestObject(){
var request_o;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_o = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_o = new XMLHttpRequest();
}
return request_o;
}
function newComment()
{
var http = createRequestObject();
var url = "leavecomment.php";
var params = "comm=hello&subject=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
showComments();
}
}
http.send(params);
}
function showComments()
{
var http = createRequestObject();
var url = 'callcomment.php';
http.open("GET", url);
var response = http.responseText;
document.getElementById('existing_comments').innerHTML = response;
}
A couple things. One thing you could test for is the status of the response. So add this to your callback function.
Code:
if(http.readyState == 4 && http.status != 200) {
alert("Got a bad response status of " + http.status);
}
The other thing I notice in your showComments() function is that it doesn't set a call back function. I believe the default is asynchronous communication so if you want to do it without a callback you'd have to explicitly say false for the 3rd parameter of open().
I wonder if the problem is you are calling to set up another http request while you are still in a handler for another one. I don't see why it would be a problem, but maybe.
how about changing the post handler again to this.
Code:
http.onreadystatechange = function() {//Call a function when the state changes.
if (http.readyState == 4) {
if (http.status == 200) {
setTimeout(showComments, 0);
}
}
}
Basically what that does is immediately generate an event to call showComments() when all other current events are handled.