0

I am working on a project where for example field number 3 on the webpage should be updated with values from a database when a user enters data into field number 1. This already works fine without any problems.

But if the user modifies field number 3 first and field number 1 at a later time, just the DOM gets updated (as I can tell from Firebug) but there isn't any visible change on field number 3 to the user.

I created a very basic version of this problem and still I am not able to tell what's wrong here.

HTML

<div id="container1">
  <textarea id="container1.1">Entry 1.1</textarea>
  <textarea id="container1.2">Entry 1.2</textarea>
  <textarea id="container1.3">Entry 1.3</textarea>
</div>

jQuery

$(document).ready(function() {
  $('textarea').change(function() {
    var clickedObject = $(this);
    var id            = $(this).attr('id').substr(9);
    var value         = $(this).val();
    var dataString    = "id=" + id + "&value=" + value;

    $.ajax({
      type: "POST",
      url: "update.php",
      data: dataString,
      cache: false,
      success: function(Result)
      {
        if(Result == '-')
        {
          console.log('Nothing to do');
        } else {
          clickedObject.next().next().html(Result);
        }
      }
    });
  });
});

PHP

<?php
  if ($_POST['id'] == '1.1') {
    echo 'Modified string';
  } else {
    echo '-';
  }
?>
URmeL
  • 11
  • 3

1 Answers1

0

You must set values of textarea by .val() method, instead of html().

And maybe it will be more descriptive if you will use only one id of textarea that should call request on changes.

Sergii Shvager
  • 1,226
  • 9
  • 14