title: How to POST data to php file using AJAX
body: <p>I am having trouble sending data to a php file to be processed. I have tried just about everything but cannot find the source of the problem. Below is a php file that sends a product name, price, and ID to a <code>checkout</code> function after a user clicks on a <em>buy</em> button.</p>
  
  <pre><code>&lt;?php
  
        $servername = "localhost";
        $username = "root";
        $password = "root";
        $dbname = "Test";
  
          // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
  
          // Check connection
        if ($conn-&gt;connect_error) {
         die("Connection failed: " . $conn-&gt;connect_error);
       } 
  
       $sql = "SELECT P1.Product_Name, S2.Price, P1.Product_ID FROM Product P1, Sale_Item S2 WHERE P1.Product_ID=S2.Product_ID AND P1.Category='Sports'";
       $res = $conn-&gt;query($sql);
       $counter=0;
  
       while ($row = $res-&gt;fetch_assoc()){
  
        $Product_Name = $row["Product_Name"];
        $Price = $row["Price"];
        $Product_ID = $row["Product_ID"];
  
        echo ('&lt;td&gt;&lt;p&gt;&lt;/p&gt;'.$row["Product_Name"].'&lt;br&gt;'.$row["Price"].'&lt;p&gt;&lt;/p&gt;&lt;input type="button" value="Buy" onclick="checkout(\'' . $Product_Name . '\', \'' . $Price . '\', \'' . $Product_ID . '\')"&lt;/td&gt;');          
        $counter++;
  
        if ($counter==3) {
          $counter=0;
          print "&lt;br&gt;";
        }
      }
  
      $conn-&gt;close();
      ?&gt;
  </code></pre>
  
  <p>And next the <code>checkout</code>function:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
          function checkout(Product_Name, Price, Product_ID) {
            //document.write(Product_Name, Price, Product_ID)
            var theProduct_Name = Product_Name;
            var thePrice = Price;
            var theProduct_ID = Product_ID;
  
            $.ajax({
              type: "POST",
              url: "http://localhost:8888/checkout.php",
              data: {Product_Name: theProduct_Name, Price: thePrice, Product_ID: theProduct_ID},
            });
  
            window.location.assign("http://localhost:8888/checkout.php")
          }
        &lt;/script&gt;
  </code></pre>
  
  <p>I am using MAMP's phpMyAdmin's database. Is my url incorrect? I've tried using <code>"http://localhost:8888/checkout.php"</code> and just <code>checkout.php</code>. Below is the php file where I need to process data. For simply learning how to send the data I am just echoing inside the file to make sure it is actually posting. But nothing is being echoed.</p>
  
  <pre><code>&lt;?php 
  
    session_start();
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "Test";
  
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
  
    // Check connection
    if ($conn-&gt;connect_error) {
     die("Connection failed: " . $conn-&gt;connect_error);
   } 
  
    $theProduct_Name = $_POST['Product_Name'];
    $theProduct_ID = $_POST['Product_ID'];
    $thePrice = $_POST['Price'];
  
   echo $theProduct_Name.$theProduct_ID.$thePrice;
  
   ?&gt; 
  </code></pre>
  
  <p>I am new to web-programming so any help or tips would be appreciated.  I've been looking at this for hours now and cannot seem to get it to work.</p>