title: Calling PHP File from Node.js with AJAX?
body: <h1>The Problem:</h1>
  
  <p>Hello, I currently have a website with a game on it that is running with node.js. It includes gulp and socket.io. </p>
  
  <p>I have a php file that I want to call from node.js, and it returns the success message (echo in file), but does not update my database.</p>
  
  <p>However, when I call the php file by putting the link in the browser or calling it with the same AJAX command from my website, it works perfectly fine.</p>
  
  <p>So from my website: site.com, the php file works perfectly, but from my node server: site.com:3000, it returns success but does not update my DB.</p>
  
  <p>I know that Node is not compatible with php but maybe there is a workaround around this.</p>
  
  <p>I also noticed that my variables are empty when calling the file from node.</p>
  
  <hr>
  
  <h1>Code:</h1>
  
  <p>Here is my php file:</p>
  
  <p><strong>subtract5.php</strong></p>
  
  <pre><code>&lt;?php
  header('Access-Control-Allow-Origin: http://cashballz.net:3000', false);
  include 'mysql.php'; 
  session_start(); 
  
  $cash_amount = $_SESSION['cash_amount'];
  $userid = $_SESSION['id'];
  $_SESSION['cash_amount'] -= 0.05;
  
  $mysql = new Mysql(); 
  
  $result = $mysql-&gt;setCashAmount($cash_amount,$userid); 
  if($result) 
  { 
  echo "5 cents have been subtracted!"; 
  } 
  else 
  { 
  session_start(); 
  session_unset(); 
  session_destroy(); 
  }
  ?&gt;
  </code></pre>
  
  <p><strong>mysql.php</strong></p>
  
  <pre><code>&lt;?php
  class Mysql 
  { 
  protected $dsn; 
  protected $username; 
  protected $password; 
  public $db; 
  
  
  function __construct() 
  { 
  //change this to your info (myDBname, myName, myPass) 
  $this-&gt;dns= 'mysql:dbname=cashball_accounts;host=localhost;charset=utf8'; 
  $this-&gt;username= 'cashball_root'; 
  $this-&gt;password= '1VeryBright*'; 
  $this-&gt;db = new PDO($this-&gt;dns, $this-&gt;username, $this-&gt;password); 
  } 
  
  
  public function setCashAmount($cash_amount, $id) 
  { 
  $sql = "UPDATE users SET cash_amount = :cash_amount - 0.05 WHERE id = :id"; 
  $stmt = $this-&gt;db-&gt;prepare($sql); 
  $stmt-&gt;bindParam(':cash_amount', $cash_amount, PDO::PARAM_STR); 
  $stmt-&gt;bindParam(':id', $id, PDO::PARAM_STR); 
  $result = $stmt-&gt;execute(); 
  return $result; 
  } 
  
  } 
  ?&gt;
  </code></pre>
  
  <p><strong>And my AJAX call: (app.js)</strong></p>
  
  <pre><code>//cut 5 cents from account - php function
      $.ajax({
      type: "POST",
      url: 'http://cashballz.net/game/5game/subtract5.php',
      data: {}, 
      success: function (data) {
          alert(data);
      }
  });
  </code></pre>
  
  <hr>
  
  <h1>Conclusion:</h1>
  
  <p>I need a way to properly run my php script and update my database directly from node.js. I have seen solutions to this with files on github, but i don't understand if this is the right solution or how to implement them in my code. Maybe there is a way to call the php file through a different domain in the background with ajax? </p>
  
  <p>I am a beginner to PHP and decent at JS, so please add detail your answers.</p>
  
  <p>Thanks for your help!</p>