title: how to use ajax to assign javascript variable to php variable
body: <p>I have the following function and I am wanting to assign the variable "Quantity" to php variable using Ajax but, I am not don't know much about ajax so can someone please give me the code that will do the job. Thanks</p>
  
  <pre class="lang-js prettyprint-override"><code>var Quantity;
  $("select").change(function() {
    var str = "";
    var price = &lt;?php echo $row[price];?&gt;;
    $("select option:selected").each(function() {
      str += $(this).text() + " ";
    });
    $(".plan_price").text("price : " + parseInt(str) * price + " baht");
    Quantity = parseInt(str);
  }).change();
  
  function selectOne() {
    var select = document.getElementById('quantity');
    for (var i=0; i&lt;&lt;?php echo $row[item_amount]?&gt;; i++) {
      select.options[select.options.length] = new Option(i+1, i);
    } 
  }
  </code></pre>
  
  <p>I want the quantity variable to be subtracted from the $row[item_amount] from the server</p>
  
  <pre class="lang-php prettyprint-override"><code>if ($row[item_amount] - Quantity  == 0) {
     $sql = "update item set active='2',item_amount=item_amount-Quantity,buy_time=NOW() where id='$id'";
  } else {
     $sql = "update item set item_amount=item_amount-Quantity,buy_time=NOW() where id='$id'";
  }
  $result = mysql_query($sql);
  </code></pre>