title: jQuery autosave not working as it should
body: <p>I have a couple of scripts i have written below that work great seperately, but together they don't work as they should. Let me post the code and then explain the issue:</p>
  
  <p>Autosave function:</p>
  
  <pre><code>&lt;script&gt;
          function autosave() {
              $('form').each(function() {
                  var string = $(this).closest('form').serialize();
                  var $this = $(this);
                  $.ajax({
                  type: "POST",
                  url: "add_room.php",
                  data: string,
                  cache: false,
                  success: function(data){
                      var saveIcon = $this.siblings('.saveIcon');
                      $this.siblings('[type=hidden]').val(data);
                      saveIcon.fadeIn(750);
                      saveIcon.delay(500).fadeOut(750);
                      $('#message').text('The id of the inserted information is ' + data);
                  }
                  });
              });
          }
          setInterval(autosave, 10 * 1000);
  &lt;/script&gt;
  </code></pre>
  
  <p>AJAX post and return script:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function() {
          $('body').on('click', '.save', function(e) {
              var string = $(this).closest('form').serialize();
              var $this = $(this);
              $.ajax({
                  type: "POST",
                  url: "add_room.php",
                  data: string,
                  cache: false,
                  success: function(data){
                      var saveIcon = $this.siblings('.saveIcon');
                      $this.siblings('[type=hidden]').val(data);
                      saveIcon.fadeIn(750);
                      saveIcon.delay(500).fadeOut(750);
                      $('#message').text('The id of the inserted information is ' + data);
                  }
              });
          });
      });
      $(document).ready(function(){
          $('#addForm').on('click', function(){
              $('&lt;form method="post" action="add_room.php"&gt;&lt;label for="itemName[]"&gt;Item&lt;/label&gt;&lt;input type="text" name="itemName[]"&gt;&lt;label for="itemPhoto[]"&gt;Photo&lt;/label&gt;&lt;input type="text" name="itemPhoto[]"&gt;&lt;input type="hidden" name="itemId[]" value=""&gt;&lt;input type="hidden" name="itemParent[]" value="&lt;?=$_GET["room"]?&gt;"&gt;&lt;div class="saveIcon" style="display: none; color: green;"&gt;SAVED!&lt;/div&gt;&lt;div class="save"&gt;Save Item&lt;/div&gt;&lt;/form&gt;').fadeIn(500).appendTo('.addItem');
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Form:</p>
  
  <pre><code>&lt;form method="post" action="add_room.php"&gt;
                  &lt;label for="itemName[]"&gt;Item&lt;/label&gt;
                  &lt;input type="text" name="itemName[]"&gt;
                  &lt;label for="itemPhoto[]"&gt;Item&lt;/label&gt;
                  &lt;input type="text" name="itemPhoto[]"&gt;
                  &lt;input type="hidden" name="itemId[]" value=""&gt;
                  &lt;input type="hidden" name="itemParent[]" value="&lt;?=$_GET['room']?&gt;"&gt;
                  &lt;div class="saveIcon" style="display: none; color: green;"&gt;SAVED!&lt;/div&gt;
                  &lt;div class="save"&gt;Save Item&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?PHP
  
      require_once('dbConfig.php');
  
      $item = $_POST['itemName'];
      $photo = $_POST['itemPhoto'];
      $id = $_POST['itemId'];
      $parentId = $_POST['itemParent'];
  
      foreach($item as $key =&gt; $val) {
  
          if(!$id[$key]) {
  
              if ($stmt = $db-&gt;prepare("INSERT test (test_title, test_desc, test_parent) VALUES (?, ?, ?)"))
              {
                  // Use an s per variable passed to the string, example - "ss", $firstname, $lastname
                  $stmt-&gt;bind_param("ssi", $val, $photo[$key], $parentId[$key]);
                  $stmt-&gt;execute();
                  $stmt-&gt;close();
  
                  echo $db-&gt;insert_id;
                  //echo "success";
              }
              // show an error if the query has an error
              else
              {
                  echo "ERROR: Could not prepare Insert SQL statement.";
              }
  
          } 
              else
          {
              if ($stmt = $db-&gt;prepare("UPDATE test SET test_title = ?, test_desc = ? WHERE test_id = ?"))
              {
                  // Use an s per variable passed to the string, example - "ss", $firstname, $lastname
                  $stmt-&gt;bind_param("ssi", $val, $photo[$key], $id[$key]);
                  $stmt-&gt;execute();
                  $stmt-&gt;close();
  
                  echo $id[$key];
                  //echo "success";
              }
              // show an error if the query has an error
              else
              {
                  echo "ERROR: Could not prepare Update SQL statement.";
              }
          }
  
      }
  
  ?&gt;
  </code></pre>
  
  <p>Now, what happens with the second script is, when used on its own without the autosave, when you fill out the form and click save it takes that forms data and saves it to the necessary rows in a database, and then returns the id of what was just saved and puts that data in a hidden field so that the php script can work out if an insert query is needed or an update query is needed(when the returned id is present). There is also a clickable div called addForm which then appends another form set below the one(s) present and again, when it's save button is clicked ONLY this form is saved/updated in the database. When i trigger the autosave like i have in my code, the autosave literally takes ALL the forms and saves them as new entries but doesn't return the id/update the hidden field to trigger the update sequence. Can you shed ANY light on this at all? It's really bugging me. Have tried explaining this as best i can, sorry it's so long. It's a bit of a complicated one! haha</p>