DJ_Plus
DJ_Plus

Reputation: 181

using jQuery to send ajax call

so im trying to get jquery to send an ajax call to update the comments database. i cant seem to find whats wrong with the code. been on the internets all day trying to find out.

<div class="comment">
<textarea name="comment" onkeydown="test(event)"></textarea>
</textarea>
</div>

    function test(event) {
    if (event.keyCode==13) {
         var comment = $('textarea[name=comment]');
        $.ajax({
          type: "GET",
          url: "commentupdate.php",
          data: "comment=" + comment,
        });
        $('.comment').hide();
        $('#container').isotope('reLayout');
    }
}

commentupdate.php

include('MasterHub_DBCon.php');
$COOKIEINFO = $_COOKIE['masterhub_user']; $USERCOOKIEINFO = explode('+', $COOKIEINFO); 
$comment = $_REQUEST['comment'];
$sql = "INSERT INTO `dmech_main`.`COMMENTS` (`ID`, `USERNAME`, `COMMENT`, `COMMENTID`) VALUES (NULL, \'' .             $COOKIEINFO['0'] . '\', \'' . $comment . '\', \'' . $_SESSION['COMMENTID'] . '\');";
mysql_query($sql);

Upvotes: 0

Views: 218

Answers (3)

Rifat
Rifat

Reputation: 7718

You missed the val() I guess. Try -

var comment = $('textarea[name=comment]').val();

Instead of -

var comment = $('textarea[name=comment]');

Upvotes: 1

poncha
poncha

Reputation: 7856

Either you need to encode the data yourself and pass a string, or pass object:

1:

<script type="text/javascript>

function test(event) {
   if (event.keyCode==13) {
      var comment = $('textarea[name=comment]');
      $.ajax({
         type: "GET",
         url: "commentupdate.php",
         data: "comment=" + encodeURIComponent(comment)
         });
      $('.comment').hide();
      $('#container').isotope('reLayout');
   }
}

</script>

2:

<script type="text/javascript>

function test(event) {
   if (event.keyCode==13) {
      var comment = $('textarea[name=comment]');
      $.ajax({
         type: "GET",
         url: "commentupdate.php",
         data: { comment: comment }
         });
      $('.comment').hide();
      $('#container').isotope('reLayout');
   }
}

</script>

Quote from http://api.jquery.com/jQuery.ajax/ :

data

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

Upvotes: 0

Trav McKinney
Trav McKinney

Reputation: 998

I would try putting the javascript inside of a

<script> 

tag to begin with.

Upvotes: 0

Related Questions