title: Send multiple AJAX data to PHP and update Mysql database
body: <p>i am tying to build an application where user can reorder items (and save the order to database). The items user is reordering are navigation links, which are generated dynamically on the page from php loop:</p>
  
  <pre><code>$nav_links.='&lt;li class="collection-item ui-state-default item" data-ord="'.$navorder.'" data-url="'.$pageurlname.'"&gt;&lt;a&gt;' .$pagename. '&lt;/a&gt;&lt;/li&gt;';}
  </code></pre>
  
  <p>$navorder is order of the page in the navigation
  $pageurlname is string which is used to call the page dynamically (index.php?page=$pageurlname) and is unique key in the table.</p>
  
  <p>I am using jqueryUi sortable funcion to make the process drag &amp; drop, it is working fine and each time i reorder the links, the new order is updated to "data-ord".. the sript:</p>
  
  <pre><code>    $('#sortable').sortable({
      stop: function(event, ui){
          $(".sortable li").each(function(i, el){
                 $(el).attr('data-ord',$(el).index()+1);
          });
      }
  });
  </code></pre>
  
  <p>Now to the problem, which is my ajax script: </p>
  
  <pre><code>$(document).on('click','.saveorder',function(){
      var neworder = $('.collection-item').attr('data-ord');
      var pgurl = $('.collection-item').attr('data-url');
      $.ajax({
          type:'POST',
          dataType:'text',
          url:'/rs/pages/nav_order.php',
          data: { neworder:neworder, pgurl:pgurl },
          success: function(data) {console.log(data); $('#response').html(data);}, 
          error: function(data) {console.log('Error!', data); } 
            });
  
      });
  </code></pre>
  
  <p>I am new to ajax, so it is mostly build on scripts i found in other quiestions here. (I was able to succesfully implement cript link this to my other functions) however it is not working in this case. The problem seems to be that i am trying to post multiple data for multiple rows (At this time i have 4 links i am trying to reorder, but the link count can be more or less). When  i tried to get values of variables "neworder" and "pgurl" (using alert), it always show only the values for the first item. </p>
  
  <p>I have tried lot of solutions found in similar quiestion but none of them worked, simply because user were posting form data and then serialized it, which is not my case because i am not sending data from the form. </p>
  
  <p>Lastly here is the nav_order.php (i guess it is wrong here too, probably need to add foreach but at first i need to have the ajax working correctly):</p>
  
  <pre><code>&lt;?php
  
  include "/rs/include/db.php";
  
  $neworder = $_POST['neworder'];
  $pgurl = $_POST['pgurl'];
  
  $query = mysqli_query($Connection, "UPDATE horus_pages SET nav_order='$neworder' WHERE url_name='$pgurl'") or die (mysqli_error($Connection));
  echo 'Reordered';
  
  ?&gt;
  </code></pre>
  
  <p>Also when i check the console, there is no data.</p>
  
  <p>So please can you tell me how to correct the ajax script to send the data for each object and then handle it correctly in the php script? Hope i described my problem clearly. Thank you for any help. </p>