title: AJAX post always saving in last data entry rather than selected data entry
body: <p>In CakePHP 3 on the View for the index function, I'm using a modal that allows users to edit specific attributes for each of the data entries in a table. When they submit those attributes, an AJAX post occurs.</p>
  
  <p>This is the table I view the data entries from:</p>
  
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th scope="col"&gt;&lt;?= $this-&gt;Paginator-&gt;sort('name', 'Name') ?&gt;&lt;/th&gt;
              &lt;th scope="col"&gt;&lt;?= $this-&gt;Paginator-&gt;sort('email', 'Email') ?&gt;&lt;/th&gt;
              &lt;th scope="col"&gt;&lt;?= $this-&gt;Paginator-&gt;sort('phoneno', 'Phone Number') ?&gt;&lt;/th&gt;
              &lt;th scope="col" class="actions"&gt;&lt;?= __('Actions') ?&gt;&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      &lt;?php foreach ($users as $user): ?&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;?= h($user-&gt;name) ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?= h($user-&gt;email) ?&gt;&lt;/td&gt;
              &lt;td&gt;&lt;?= h($user-&gt;phoneno) ?&gt;&lt;/td&gt;
              &lt;td class="actions" style="color: #0f4bac"&gt;
                  &lt;?= $this-&gt;Html-&gt;link(__('View'), ['action' =&gt; 'popup', $user-&gt;id], ['class' =&gt; 'view', 'data-id' =&gt; $user-&gt;id]) ?&gt;
                  &lt;?= $this-&gt;Html-&gt;link(__('Change Photo'), ['action' =&gt; 'popup', $user-&gt;id], ['class' =&gt; 'managephoto', 'data-id' =&gt; $user-&gt;id]) ?&gt;
  
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;?php endforeach; ?&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>This is the corresponding script that is triggered when I click Change Photo for a data entry:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(function () {
          $('.managephoto').click(function (ev) {
              ev.preventDefault();
              var userId = $(this).attr('data-id');
              $('#photoModal').modal('show');
              $.ajax({
                  url:"localhost/project/users/details/"+userId+".json",
                  type:'POST',
                  success:function(res) {
                      if(res) {           
                          $('#photo').val(res.photoLink);                                                  
                          $('#text').val(res.text);
                      }
                  }
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><code>url:"localhost/project/users/details/"+userId+".json"</code> references this function, to fetch any data for the fields in the modal (if any):</p>
  
  <pre><code>public function details($id = null)
      {
          $details = $this-&gt;Users-&gt;get($id);
          $this-&gt;set(array(
              'output' =&gt; $details,
              '_serialize' =&gt; 'output',
              '_jsonp'=&gt;true
          ));
      }
  </code></pre>
  
  <p>The modal then opens up:</p>
  
  <pre><code>&lt;div class="modal fade" id="photoModal" tabindex="-1" role="dialog" aria-labelledby="photoModalLabel" aria-hidden="true"&gt;
      &lt;div class="modal-dialog" role="document"&gt;
          &lt;div class="modal-content"&gt;
              &lt;div class="modal-header"&gt;
                  &lt;button type="button" class="close" data-dismiss="modal" aria-label="Close"&gt;
                      &lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;h3 class="modal-title" id="photoModalLabel"&gt;Change Photo&lt;/h3&gt;
                  &lt;br&gt;
                  &lt;div&gt;
                      &lt;div class="col-sm-12"&gt;
                          &lt;?= $this-&gt;Form-&gt;input('photoLink', ['class' =&gt; 'form-control', 'label' =&gt; 'Photo Link', 'placeholder' =&gt; 'Link to photo', 'id' =&gt; 'photoLink]); ?&gt;
                      &lt;/div&gt;
                      &lt;div class="col-sm-12"&gt;
                          &lt;?= $this-&gt;Form-&gt;input('text', ['type' =&gt; 'textarea', 'class' =&gt; 'form-control', 'label' =&gt; 'Photo Caption', 'placeholder' =&gt; 'Description', 'id' =&gt; 'text']); ?&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="modal-footer"&gt;
                  &lt;button type="button" class="btn btn-secondary" data-dismiss="modal"&gt;Close
                  &lt;/button&gt;
                  &lt;button id="savebutton" type="button" class="btn btn-primary" data-dismiss="modal"&gt;Save changes
                  &lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>When I click the Save Changes button, this script is triggered and the AJAX post occurs:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
          $("#savebutton").click(function () {
              $.post("localhost/project/users/edit/&lt;?= $user-&gt;id ?&gt;.json", {
                  photoLink: $("#photo").val(),
                  text: $("#text").val()
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>However, while the AJAX post does work and it does edit only the fields in the modal (everything else is untouched and fields not in the modal don't go blank), the data entry being edited is always the last data entry, and not the selected entry (eg. if I have 2 data entries in the Users table, entry 2 will always be edited, even if I selected data entry 1 to Edit).</p>
  
  <p>Edit: Added the table, the script which opens up the modal and the modal itself.</p>