简体   繁体   中英

Calling PHP File from Node.js with AJAX?

The Problem:

Hello, I currently have a website with a game on it that is running with node.js. It includes gulp and socket.io.

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.

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.

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.

I know that Node is not compatible with php but maybe there is a workaround around this.

I also noticed that my variables are empty when calling the file from node.


Code:

Here is my php file:

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 "5 cents have been subtracted!"; 
} 
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= 'cashball_root'; 
$this->password= '1VeryBright*'; 
$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; 
} 

} 
?>

And my AJAX call: (app.js)

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

Conclusion:

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?

I am a beginner to PHP and decent at JS, so please add detail your answers.

Thanks for your help!

so what I'm wondering

$result = $stmt->execute(); 

what will the sql statement be when called from Node? Can you debug that (when it's called from Node)

My guess would be that

$_SESSION

is empty when called from Node

As you state in some comment the $_SESSION variables are empty. Assuming that you have the same data (id and cash amount) available in your node.js you could pass it along with the AJAX call, something like this:

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

You can then access the variables that you passed along via $_POST in your php file -- because of type: "POST" . Something along these lines:

<?php
header('Access-Control-Allow-Origin: http://cashballz.net:3000', false);
include 'mysql.php';

$cash_amount = $_POST['cash_amount'];
$userid = $_POST['id'];
$new_cash_amount = $cash_amount - 0.05;

$mysql = new Mysql();

$result = $mysql->setCashAmount($cash_amount, $userid);
if ($result) {
    echo json_encode([
        'new_cash_amount' => $new_cash_amount,
        'message' => '5 cents have been subtracted!'
    ]);
} else {
    echo json_encode(['error' => 'Huge error.']);
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM