Search code examples
phpjqueryajaxcodeigniterm

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

});
 }
    });


}

Solution

  • $(".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();
          }
    
       });
     }
    

    });