简体   繁体   English

如何获取JavaScript全局变量并将其发送到php文件?

[英]How do I get a JavaScript global variable and send it into a php file?

I am building a game using phaser game development and I want to send the score into mySQL database but JS and PHP don't directly communicate. 我正在使用Phaser游戏开发来开发游戏,我想将分数发送到mySQL数据库中,但是JS和PHP无法直接通信。 I am using AJAX but I'm stuck. 我正在使用AJAX,但遇到问题。 Here is my code: 这是我的代码:

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8" />
<title>Star Runner</title>
<script>window.score=0;</script>
<script src="phaser.js"></script>

</head>
<body>

<script type="text/javascript">

// game goes here

</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    $(document).ready(function(){
        $.ajax({
            url: "inscore.php",
            type: 'POST',
            data: 'score='+score,
            cache: false,
            success: function(data) {
                    alert(data);}
        });

    });



</script>

<h1 style="color:white">Star Runner</h1

</body>
</html>

My php file 'inscore.php' would be: 我的php文件'inscore.php'将是:

<?php

include 'login.php';
$score = $_GET['score'];

echo "<p>". $score. "</p>"; 
//SQL queries

?>

I'm not sure if I should link this page to the php page because I just want the score to update into my database immediately after the game is done. 我不确定是否应该将此页面链接到php页面,因为我只想在游戏完成后立即将比分更新到我的数据库中。 Can someone please help me? 有人可以帮帮我吗?

First, there is a little mistake in your code. 首先,您的代码中有一点错误。

Your Code: 您的代码:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> // code here will not run $(document).ready(function() { $.ajax({ // ... }); }); </script> 

It suppose to be: 它应该是:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $.ajax({ // ... }); }); </script> 

And here is a example for you: 这是为您提供的示例:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Star Runner</title> <script src="phaser.js"></script> </head> <body> <h1 style="color:white">Star Runner</h1> <div> Game stage ... </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> // game goes here var Game = { score: 0, run: function() { // your awesome code if (true) { this.gameOver(); } }, gameOver: function() { var _this = this; // send the request $.ajax({ url: "inscore.php", type: 'POST', data: 'score=' + this.score, cache: false, success: function(data) { // balabala console.log(_this); } }); } }; // Run the game Game.run(); </script> </body> </html> 

It is better to use OOP (Object Oriented Programming), especially in the game project. 最好使用OOP(面向对象编程),尤其是在游戏项目中。

Since you are posting data on your ajax, you should get the data on PHP's $_POST global variable 由于要在ajax上发布数据,因此应该在PHP的$ _POST全局变量中获取数据

<?php

include 'login.php';
$score = $_POST['score'];

echo "<p>". $score. "</p>"; 
//SQL queries

?>

And try to use a php framework like laravel or symfony for this project, even a simplier one like slim would be better for you to get things done 并尝试为此项目使用laravel或symfony这样的php框架,即使是苗条的简单框架也可以帮助您更好地完成工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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