title: How can i set the value of a hidden field after successful ajax post?
body: <p>I've got some code that works nicely for saving to the database etc. What i'd like to do now is after the fields are saved, i want the echoed last id to be populated to a hidden field so i can use that to determine any future insert/update queries.</p>
  
  <p>My form is:</p>
  
  <pre><code>&lt;div id="formHolder"&gt;
  &lt;form type="post" action="add_room.php" id="mainForm"&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="hiddenId[]" value=""&gt;
      &lt;div class="save"&gt;Save Item&lt;/div&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>my jQuery is:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function() {
          $('body').on('click', '.save', function(e) {
              var string = $(this).closest('form').serialize();
              $.ajax({
                  type: "POST",
                  url: "add_room.php",
                  data: string,
                  cache: false,
                  success: function(data){
                      $('#message').text('The id of the inserted information is ' + data);
                  }
              });
          });
      });
      $(document).ready(function(){
          $('#addForm').on('click', function(){
              $('&lt;form&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="hiddenId[]" value=""&gt;&lt;div class="save"&gt;Save Item&lt;/div&gt;&lt;/form&gt;').fadeIn(500).appendTo('#formHolder');
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>and finally my php is:</p>
  
  <pre><code>&lt;?PHP
  
      include('dbConfig.php');
  
      $item = $_POST['itemName'];
      $photo = $_POST['itemPhoto'];
  
      foreach($item as $key =&gt; $val) {
  
          if ($stmt = $db-&gt;prepare("INSERT test (test_title, test_desc) VALUES (?, ?)"))
          {
              // Use an s per variable passed to the string, example - "ss", $firstname, $lastname
              $stmt-&gt;bind_param("ss", $val, $photo[$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 SQL statement.";
          }
      }
  
  ?&gt;
  </code></pre>
  
  <p>Everything works nicely for adding field data to the database, adding extra fields etc. I just cannot get it to save the echoed id of each ajax post to save to the hidden field, yet it saves it to the #message div no problem. Any ideas? I have tried using .val(); but it didn't work, i'm stumped</p>
  
  <p>Andy</p>