Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Patrick Karkafi
Patrick Karkafi
508 Points

AJAX PHP Is Not Working in Node.js.

When i put a php file link in the browser, it works perfectly fine and updates the database as expected.

When i call it with ajax from node, it echos the success message but the DB does not update.

The Session variables are undefined, so i changed them to real numbers and the code still did not work. What is going wrong?

My PHP Code:

subtract5.php:

<?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->setCashAmount($cash_amount,$userid); 
if($result) 
{ 
echo $cash_amount; 
} 
else 
{ 
session_start(); 
session_unset(); 
session_destroy(); 
}
?>

mysql.php:

<?php
class Mysql 
{ 
protected $dsn; 
protected $username; 
protected $password; 
public $db; 


function __construct() 
{ 
//change this to your info (myDBname, myName, myPass) 
$this->dns= 'mysql:dbname=cashball_accounts;host=localhost;charset=utf8'; 
$this->username= 'myUser'; 
$this->password= 'myPass'; 
$this->db = new PDO($this->dns, $this->username, $this->password); 
} 


public function setCashAmount($cash_amount, $id) 
{ 
$sql = "UPDATE users SET cash_amount = :cash_amount - 0.05 WHERE id = :id"; 
$stmt = $this->db->prepare($sql); 
$stmt->bindParam(':cash_amount', $cash_amount, PDO::PARAM_STR); 
$stmt->bindParam(':id', $id, PDO::PARAM_STR); 
$result = $stmt->execute(); 
return $result; 
} 

} 
?>

My node.js app.js AJAX:

//cut 5 cents from account - php function
        $.ajax({
        type: "POST",
        url: 'http://cashballz.net/game/5game/subtract5.php',
        data: {}, 
        success: function (data) {
            alert(data);
        }
    });

Recap:

  • PHP File works from site.com with the same AJAX code, and by putting its link in the browser.
  • PHP File is executed from node, but does not update the DB
  • PHP File has undefined $_SESSION variables but when replaced, the DB still does not update

Possible solutions:

  • Is there extra code i can put in AJAX to tell it to "pipe" the call to site.com to call the file instead of calling it straight from node?

Extra Info:

  • No errors in error logs or console
  • I need to use AJAX to input data into the file later on
  • Solution must be secure

Thanks for the help!

1 Answer

You may be over complicating this. The database already has the current 'cash amount', so there is no need to maintain this state in the $_SESSION. When the request comes in from the front end just subtract the .05 from the row in the table and return the new current amount. Then send that back to the front end and on the front end get it from the response object.

Happy coding

  • Ben