title: Save WooCommerce Order details in custom table
body: <p>I have created a custom database in table <code>order-master</code> in which I will save WooCommerce order <code>total amount</code>, <code>shipping charges</code> and <code>token</code>.
  For this, I have edited <code>paypemts.php</code> file, code is below:</p>
  
  <pre><code>&lt;?php
  include_once($_SERVER['DOCUMENT_ROOT'].'/canadiapharma.com/wp-config.php' );
  global $woocommerce;
  global $wpdb;
  echo '&lt;h3&gt;Custom Calculation :&lt;/h3&gt;';
  $amount_2 = $woocommerce-&gt;cart-&gt;get_cart_total();
  $ship_raw = floatval( preg_replace( '#[^\d.]#', '', $woocommerce-&gt;cart-&gt;get_cart_shipping_total() ) );
  $ship = $ship_raw - 3600;
  echo '&lt;strong&gt;Shipping :&lt;/strong&gt;';
  echo $ship; 
  $nano=WC()-&gt;cart-&gt;cart_contents_total;
  echo '&lt;br&gt;';
  $total_amt = $nano+$ship;
  echo '&lt;strong&gt;Total :&lt;/strong&gt;';
  echo $total_amt;
  echo '&lt;br&gt;';
  $salt = 'dAta_EnC!=';
  $token_raw = base64_encode($total_amt. $salt);
  $token = preg_replace(sprintf('/=/', $salt), '', $token_raw);
  echo '&lt;strong&gt;Token :&lt;/strong&gt;';
  echo $token;
  $wpdb-&gt;query("INSERT INTO order_master (payment_amt, ship, token) VALUES ('$total_amt', '$ship', '$token')"  );
  ?&gt;
  </code></pre>
  
  <p>This code works fine and stores the data to the database. But it stores the data as soon as the <code>checkout.php</code> page loads. So, I made some changes and created an <code>onclick</code> event on <code>Place Order</code> button in <code>checkout.php</code> page.</p>
  
  <p>OnCLick Event:</p>
  
  <pre><code>&lt;?php echo apply_filters( 'woocommerce_order_button_html', '&lt;button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="' . esc_attr( $order_button_text ) . '" data-value="' . esc_attr( $order_button_text ) . '" onclick="dbinsert()"&gt;' . esc_html( $order_button_text ) . '&lt;/button&gt;' ); 
          ?&gt;
  </code></pre>
  
  <p>dbinsert():</p>
  
  <pre><code>&lt;script&gt;
          function dbinsert() {
              var myKeyVals = { amount : &lt;?php echo $total_amt; ?&gt;., ship : &lt;?php echo $ship; ?&gt;., token : &lt;?php echo $token; ?&gt; }
  
              var saveData = $.ajax({
                          type: 'POST',
                          url: "savedata.php",
                          data: myKeyVals,
                          dataType: "text",
                          success: function(dbinsert) { alert("Save Complete") }
              });
              saveData.error(function() { alert("Something went wrong"); });
                      }
          &lt;/script&gt;
  </code></pre>
  
  <p><code>onclick</code> event executes perfectly but doesn't know why data is not going into the database. Below is my <code>savedata.php</code> file.</p>
  
  <pre><code>&lt;?php
  global $wpdb;
  $total_amt = $_POST['amount'];
  $ship = $_POST['ship'];
  $token = $_POST['token'];
  $stmt = $wpdb-&gt;query("INSERT INTO order_master (payment_amt, ship, token) VALUES ('$total_amt', '$ship', '$token')"  );
  $stmt-&gt;bindparam('payment_amt', $total_amt);
  $stmt-&gt;bindparam('ship', $ship);
  $stmt-&gt;bindparam('token', $token);
  if($stmt-&gt;execute())
  {
    $res="Data Inserted Successfully:";
    echo json_encode($res);
  }
  else {
    $error="Not Inserted,Some Probelm occur.";
    echo json_encode($error);
  }
  ?&gt;
  </code></pre>
  
  <p>Please help me to fix this issue.</p>