title: jQuery ajax post to php script & update mysql database
body: <p>I have a page that shows inventory. When inventory becomes defective (or any other status) I would like to select that option from a dropdown, and update the database accordingly without a refresh of the page. I have been playing around with .ajax and jQuery and I think I am close I just cant get it to quite work properly. </p>
  
  <p>First, here is the code that populates the dropdown. There is nothing wrong with this code (used in other places in my site). Just though I would include for name and id purposes</p>
  
  <pre><code>$myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.InventoryItemStatusDescription FROM InventoryItemStatus";
  $myConnection = mysql_connect($myServer,$myUsername,$myPassword);
  if (!$myConnection){
      die('Conncetion Failed:' . mysql_error());
  }
  @mysql_select_db($myDatabase) or die("Unable to select database");
  $result2 = mysql_query($myQuery2) or die (mysql_error());
  
  if(mysql_num_rows($result2)){ 
      $select2= '&lt;select style="width:90px" id="StatusList" name="StatusList"&gt;';
      $select2.='&lt;option value="0.5"&gt; --Status-- &lt;/option&gt;';
          while($rs=mysql_fetch_array($result2)){
              $select2.='&lt;option value="'.$rs['InventoryItemStatusID'].'"&gt;'.$rs['InventoryItemStatusDescription'].'&lt;/option&gt;';
              }
  } 
  $select2.='&lt;/select&gt;';
  </code></pre>
  
  <p>Here is the jQuery code I am using. The .change works okay. (If I replace the $.ajax method with an alert, when I change the dropdown, it alerts a popup)</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function(){
    $('[name="StatusList"]').change(function() {
      var mydata = $('[name="StatusList"]').val();//$(this).val();
      var inputdata = $('[name="StarmontInventoryItemID"]').val();
      $.ajax({   
         type: 'POST',   
         url: 'update_status/update_starmont.php',   
         data: {StatusList:mydata ,StarmontInventoryItemID:inputdata}
      });
    });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>Here is the form included in my php code. This includes a hidden field which includes the unique ID of the inventory item that I would like to change the status of, and the dropdown menu.</p>
  
  <pre><code>    &lt;form method='post'&gt;
      {$select2}
      &lt;input type='hidden' value='{$row14['InventoryItemID']}' name='StarmontInventoryItemID' id='StarmontInventoryItemID'/&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>This is the php script that is called on change. I think this is the part that I am not using properly. I am just unclear on how the data is passed to this script. I cant figure out if it is passed in the $_POST variable or not. </p>
  
  <pre><code>include_once('../../../php/common.php');
          include_once('../../../php/db.php');
  
          $InventoryItemID = $_POST['StarmontInventoryItemID'];
          //$InventoryItemStatusID = $_POST['StatusList'];
  
          mysql_query("UPDATE InventoryItem SET InventoryItemStatusID = 2 WHERE InventoryItemID = '$InventoryItemID'") or die (mysql_error());
  </code></pre>
  
  <p>As you can see, I am simply trying to pass the inventory item id to the script and update that item to a status of '2'. My end result is to update it to whatever the value of the dropdown menu is, but I am sure if I can just get this part working I can figure out the rest.</p>
  
  <p>Thank you in advance for any help!
  Bonne journée!</p>
  
  <p>EDIT</p>
  
  <p>This works perfectly thank you so much. I have run into another issue unfortunately. For example, I have 10 inventory items. I do complex query to retrieve all the information needed in an array, then I do a foreach. It seems like the inventory item at the top works correctly updating the status, but any item below it does not execute the sql query for an update.</p>
  
  <pre><code>if (sizeof($rows14) &gt; 0) {
  foreach($rows14 as $row14):
  
  $query17 = "
  SELECT 
      SystemID,
      Serial
  FROM 
      Device 
  WHERE 
      Device.InventoryItemID = :InventoryItemID
  ";
  $query_params17 = array( 
      ':InventoryItemID' =&gt; $row14['InventoryItemID']
  );
  try { 
      $stmt17 = $db-&gt;prepare($query17); 
      $stmt17-&gt;execute($query_params17); 
  } 
  catch(PDOException $ex) { 
      die("Failed to run query: " . $ex-&gt;getMessage()); 
  }
  $row17 = $stmt17-&gt;fetch();
  
  
  $myQuery2 = "SELECT InventoryItemStatus.InventoryItemStatusID, InventoryItemStatus.InventoryItemStatusDescription FROM InventoryItemStatus";
  $result2 = mysql_query($myQuery2) or die (mysql_error());
  if(mysql_num_rows($result2)){ 
      $select2= '&lt;select style="width:90px" name="StatusList"&gt;';
          while($rs=mysql_fetch_array($result2)){
              if($rs['InventoryItemStatusID'] == $row14['Status']){ 
                  $select2.='&lt;option value="'.$rs['InventoryItemStatusID'].'" selected="'.$row14['Status'].'"&gt;'.$rs['InventoryItemStatusDescription'].'&lt;/option&gt;'; 
              }
              else{
                  $select2.='&lt;option value="'.$rs['InventoryItemStatusID'].'"&gt;'.$rs['InventoryItemStatusDescription'].'&lt;/option&gt;';                        
              }
              }
  } 
  $select2.='&lt;/select&gt;';
  
  echo"
  &lt;tr class='tableRowClass2'&gt;
  &lt;td height='20'&gt;{$row14['OnHand']}&lt;/td&gt;
  &lt;td height='20'&gt;{$row14['ItemName']} - {$row17['SystemID']} - {$row17['Serial']}&lt;/td&gt;
  &lt;td height='20'&gt;
      &lt;form method='post'&gt;
      {$select2}
      &lt;input type='hidden' value='{$row14['InventoryItemID']}' name='StarmontInventoryItemID'/&gt;
      &lt;/form&gt;
  &lt;/td&gt;
  &lt;/tr&gt;";
  endforeach;
  }
  </code></pre>
  
  <p>I have tried multiple different ways to get this to work with all of the inventory items. I am unsure if it is valid or not to put this within the foreach loop or not. Again any help is greatly appreciated!</p>