我使用名为Staroids的冲击游戏引擎创建了一个游戏。
我创建了一个数据库来存储高速缓存。
我已经按照上面的答案做了,并研究了其他一些解决方案,但似乎不起作用!
我正在尝试运行一个php文件,其中包含以下代码:
<?php
$connect = mysql_connect('localhost', 'root', 'password');
mysql_select_db('staroids', $connect);
$sql = 'INSERT INTO staroids (score) VALUES ("'.$_POST['score'].'")';
$result = mysql_query($sql);
?>
以下是我在JavaScript文件中运行的代码:
$.post('scripts/register.php', {score: score}, function(){}).error(function(){
alert('error... ohh no!');
});
当它到达此代码时,在控制台中会出现以下错误:
Uncaught ReferenceError: $ is not defined
您可能没有正确地将jQuery加载到页面中。确保您的头中有加载jQuery的脚本标记,并进行故障排除,以确保它实际上正在加载jQuery。
我在上面评论的帮助下解决了这个问题,并使用了大量的尝试和错误,下面是工作代码:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
//make sure you run this otherwise the $post request wont work!
然后在我想运行代码的JavaScript函数中:
$.post('register.php', {score: this.score}, function(){}).error(function(){
alert('error... ohh no!');
});
//this runs the php file parsing through the score variable
下面是我运行的PHP代码,将解析后的through变量添加到数据库中的表中:
<?php
$score = $_POST['score'];
mysql_connect("localhost", "root", "password");
mysql_select_db("staroids");
mysql_query("INSERT INTO scores (score) VALUES ('$score')");
?>