2

I have created a game in using impact game engine called Staroids.

I have created a database to store the highscores.

I have done what it says on this previous answer and looked at a few other solutions, but it doesn't seem to work!

I am trying to run a php file which contains the following code:

<?php

    $connect = mysql_connect('localhost', 'root', 'password');
    mysql_select_db('staroids', $connect);

    $sql = 'INSERT INTO staroids (score) VALUES ("'.$_POST['score'].'")';
    $result = mysql_query($sql);

?>

Here is the code that I run in the JavaScript file:

$.post('scripts/register.php', {score: score}, function(){}).error(function(){
    alert('error... ohh no!');
});

It comes up with the following error in console when it reaches this code:

Uncaught ReferenceError: $ is not defined 
Community
  • 1
  • 1
smj2393
  • 1,869
  • 1
  • 20
  • 48

2 Answers2

1

You're probably not loading jQuery into your page correctly. Make sure you have the script tag in the head that loads jQuery and troubleshoot to make sure it's actually loading jQuery at all.

Samuel Reid
  • 1,708
  • 11
  • 21
  • Yep jQuery is running, I think it must be something to do with the game engine impact.js, I'll post on their forums, thanks though! – smj2393 Aug 02 '13 at 19:14
  • Go into your console and just type `$` and tell me what happens. You may just need to add `jQuery.noConflict()` – Samuel Reid Aug 02 '13 at 19:16
  • Right. If `$` refers to jQuery, it comes out like this: `function (e,t){return new x.fn.init(e,t,r)};`. Try typing `jQuery` and see if you get that. If so, then jQuery is loading but not using `$`. In fact, to make things simple, just type `$ == jQuery` and if it says "false" then that's your problem. – Samuel Reid Aug 02 '13 at 19:56
  • My bad, I ran it while jquery was commented out, it outputs `function ( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context, rootjQuery ); }` when I type $ and when I type jQuery `jQuery function ( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context, rootjQuery ); }` – smj2393 Aug 02 '13 at 19:59
  • Don't know how, but it seems to be working now... Well no more console error, but it still isn't posting to the database, Cheers for the help @Samuel Reid – smj2393 Aug 02 '13 at 20:05
0

I solved it with the help from the comments above and using a lot of trial and error, here is the working code:

<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!

Then where in the JavaScript function where I wanted to run the code I put:

$.post('register.php', {score: this.score}, function(){}).error(function(){
    alert('error... ohh no!');
});
//this runs the php file parsing through the score variable

Here is the PHP code which I then run to add the parsed through variable to the table in the database:

<?php

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

?>
smj2393
  • 1,869
  • 1
  • 20
  • 48