Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts

    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>

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts
    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;
    }

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts
    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

  • #8
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts
    I added that, and I dont get any alerts at all :S

  • #10
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    Regular Coder
    Join Date
    Sep 2006
    Posts
    122
    Thanks
    4
    Thanked 2 Times in 2 Posts
    You're a legend!

    Works perfectly

    thanks so much!!

  • #13
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great! Glad we were able to figure it out. :)

    david_kw


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •