Dinu Abraham
Dinu Abraham

Reputation: 31

How to save data in textarea on enter key using ajax like facebook comment?

I have done the code like this in view page and controller done in codeigniter While pressing enter key multiple times data saving to the table. Can anybody help to solve this problem

View Page

<form>
<div class="cmt-box">
<textarea class="form-control" name="txtArea" id="txtArea<?php echo $row->id;?>" onkeypress="onTestChange(1)" rows="1"></textarea>
</div></form>

script

function onTestChange(id) { 
  $("#txtArea"+id).keypress(function(e) {  
   if(e.which == 13) {

dataString=document.getElementById("txtArea"+id).value;
$.ajax({
  type: "POST",
  url: "<?php echo site_url('show/insertcomment'); ?>",
  data: { comment :dataString, id:id},
  success: function(data){
 location.reload();
  }

});
 }
    });


}

Upvotes: 0

Views: 1023

Answers (2)

Disha V.
Disha V.

Reputation: 1864

HTML:

<div class="cmt-box">
    <textarea class="form-control mytext" name="txtArea" id="txtArea<?php echo $row->id;?>" rows="1"></textarea>
</div>

Jquery:

$(document).ready(function(){
    $('.mytext').keyup(function (evt) {
        evt = evt || window.event;
        if (evt.keyCode == 13) { /* pressed enter key */
            $.ajax({
                type: "POST",
                url: "<?php echo site_url('show/insertcomment'); ?>",
                data: { comment :dataString, id:id},
                success: function(data){
                    location.reload();
                }
            });
        }
    });
});

Upvotes: 0

Bhavesh Patel
Bhavesh Patel

Reputation: 26

$(".class_txtarea").keypress(function(e) {  
   if(e.which == 13) {

     dataString=document.getElementById(this).value;
     $.ajax({
      type: "POST",
      url: "<?php echo site_url('show/insertcomment'); ?>",
      data: { comment :dataString, id:id},
      success: function(data){
          location.reload();
      }

   });
 }

});

Upvotes: 1

Related Questions