2013-08-02 34 views
2

我使用影响游戏引擎Staroids创建了一款游戏。运行影响引擎中的JavaScript代码

我创建了一个数据库来存储高分。

我已经完成了它在前面的answer上所说的内容,并着眼于其他一些解决方案,但似乎并不奏效!

我试图运行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 
+0

将“'。$ _ POST ['score']。'”更改为''。$ _ POST ['score']。''' –

+0

请勿使用mysql扩展名。这是贬值的,有更好的选择。 – shmuli

回答

0

我从上述评论的帮助下解决了它,并使用了大量的试验和错误的,这里是工作代码:

<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代码,然后我通过运行变量添加解析到数据库中的表:

<?php 

    $score = $_POST['score']; 
    mysql_connect("localhost", "root", "password"); 
    mysql_select_db("staroids"); 
    mysql_query("INSERT INTO scores (score) VALUES ('$score')"); 

?> 
1

你可能不会加载j到y我们的页面正确。确保你在头部加载了jQuery的脚本标记并进行故障排除,以确保它实际上加载了jQuery。

+0

是的jQuery jQuery正在运行,我认为它必须与游戏引擎impact.js有关,我会在他们的论坛发帖,谢谢! – smj2393

+0

进入你的控制台,只需输入'$'并告诉我会发生什么。您可能只需要添加'jQuery.noConflict()' –

+0

控制台输出'函数$(){[命令行API]}' – smj2393