...

Send comment to mysql database

tribalmaniac
04-02-2007, 11:41 PM
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 -var params = "comm=hello&subject=binny";

rather than what's in the text box, so ignore that!
Here's what i have so far

<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>

david_kw
04-03-2007, 07:11 AM
Couple questions

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.

david_kw

tribalmaniac
04-03-2007, 09:42 AM
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:


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;
}

david_kw
04-03-2007, 09:53 AM
A couple things. One thing you could test for is the status of the response. So add this to your callback function.


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().

http.open("GET", url, false);

david_kw

tribalmaniac
04-03-2007, 10:43 AM
I added the alert and added the "false".
The alert doesn't give any response, any ideas?

david_kw
04-03-2007, 05:36 PM
Try this in your handler to see if the browser is getting any return and what kind.


http.onreadystatechange = function() {//Call a function when the state changes.
if (http.readyState == 4) {
alert("status = '" + http.status + "'");
alert("response = '" + http.responseText + "'");
if (http.status == 200) {
showComments();
}
}
}


That should find out if the problem is in the post or the get portion of the code.

david_kw

tribalmaniac
04-03-2007, 07:00 PM
i get

"status = '200'"
"response = ''"

but also, from the leavecomment.php script there isn't any response, it's just a script that inserts the text into the database

Still not quite sure what to do though

Thanks for all your help so far :)

david_kw
04-03-2007, 07:24 PM
Ahh ok. So post is working as expected.

Try changing showComments()


function showComments()
{
var http = createRequestObject();
var url = 'callcomment.php';
http.open("GET", url, false);
alert("status = '" + http.status + "'");
alert("response = '" + http.responseText + "'");
var response = http.responseText;
document.getElementById('existing_comments').innerHTML = response;
}


david_kw

tribalmaniac
04-03-2007, 09:00 PM
I added that, and I dont get any alerts at all :S

david_kw
04-03-2007, 11:14 PM
Hmm, not a good sign. :)

So are you getting a javascript error?

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.


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.

What browser are you testing in again?

david_kw

david_kw
04-03-2007, 11:16 PM
Ok, I'm an idiot. I just noticed a problem. You weren't calling http.send(null); in your showComments() function.


function showComments()
{
var http = createRequestObject();
var url = 'callcomment.php';
http.open("GET", url, false);
http.send(null);
alert("status = '" + http.status + "'");
alert("response = '" + http.responseText + "'");
var response = http.responseText;
document.getElementById('existing_comments').innerHTML = response;
}


david_kw

tribalmaniac
04-04-2007, 12:21 AM
You're a legend!

Works perfectly :D

thanks so much!!

david_kw
04-04-2007, 12:51 AM
Great! Glad we were able to figure it out. :)

david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum