Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 14 votes, 3.14 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2007, 11:41 PM   PM User | #1
tribalmaniac
Regular Coder

 
Join Date: Sep 2006
Posts: 122
Thanks: 4
Thanked 2 Times in 2 Posts
tribalmaniac is an unknown quantity at this point
Send comment to mysql database

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>
tribalmaniac is offline   Reply With Quote
Old 04-03-2007, 07:11 AM   PM User | #2
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
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
david_kw is offline   Reply With Quote
Old 04-03-2007, 09:42 AM   PM User | #3
tribalmaniac
Regular Coder

 
Join Date: Sep 2006
Posts: 122
Thanks: 4
Thanked 2 Times in 2 Posts
tribalmaniac is an unknown quantity at this point
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;
}
tribalmaniac is offline   Reply With Quote
Old 04-03-2007, 09:53 AM   PM User | #4
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
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().

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

david_kw
david_kw is offline   Reply With Quote
Old 04-03-2007, 10:43 AM   PM User | #5
tribalmaniac
Regular Coder

 
Join Date: Sep 2006
Posts: 122
Thanks: 4
Thanked 2 Times in 2 Posts
tribalmaniac is an unknown quantity at this point
I added the alert and added the "false".
The alert doesn't give any response, any ideas?

Last edited by tribalmaniac; 04-03-2007 at 01:10 PM..
tribalmaniac is offline   Reply With Quote
Old 04-03-2007, 05:36 PM   PM User | #6
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
Try this in your handler to see if the browser is getting any return and what kind.

Code:
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
david_kw is offline   Reply With Quote
Old 04-03-2007, 07:00 PM   PM User | #7
tribalmaniac
Regular Coder

 
Join Date: Sep 2006
Posts: 122
Thanks: 4
Thanked 2 Times in 2 Posts
tribalmaniac is an unknown quantity at this point
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
tribalmaniac is offline   Reply With Quote
Old 04-03-2007, 07:24 PM   PM User | #8
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
Ahh ok. So post is working as expected.

Try changing showComments()

Code:
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
david_kw is offline   Reply With Quote
Old 04-03-2007, 09:00 PM   PM User | #9
tribalmaniac
Regular Coder

 
Join Date: Sep 2006
Posts: 122
Thanks: 4
Thanked 2 Times in 2 Posts
tribalmaniac is an unknown quantity at this point
I added that, and I dont get any alerts at all :S
tribalmaniac is offline   Reply With Quote
Old 04-03-2007, 11:14 PM   PM User | #10
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
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.

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.

What browser are you testing in again?

david_kw
david_kw is offline   Reply With Quote
Old 04-03-2007, 11:16 PM   PM User | #11
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
Ok, I'm an idiot. I just noticed a problem. You weren't calling http.send(null); in your showComments() function.

Code:
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
david_kw is offline   Reply With Quote
Old 04-04-2007, 12:21 AM   PM User | #12
tribalmaniac
Regular Coder

 
Join Date: Sep 2006
Posts: 122
Thanks: 4
Thanked 2 Times in 2 Posts
tribalmaniac is an unknown quantity at this point
You're a legend!

Works perfectly

thanks so much!!
tribalmaniac is offline   Reply With Quote
Old 04-04-2007, 12:51 AM   PM User | #13
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
Great! Glad we were able to figure it out. :)

david_kw
david_kw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 PM.


Advertisement
Log in to turn off these ads.