0

I have a website where you need to create an account and login to play the game. I have PHP that refers to a MySQL database with a column for UserId, Username, firstname, lastname, password and score. The login works fine. Then, you are taken to the html document that contains the game. It is somewhat like cookie clicker, where your objective is to get the highest score possible through interacting with an object.

I have a score variable called "clicks" which increases quite rapidly.

I have some javascript code that reads;

if (clicks%5==0) {
    sendScore()
    } 

What this means is when clicks is divisibly by 5 it activates a function called sendScore. It activates the function every 5 increments because I assume sending data to the table multiple times a second would be demanding too much from the server. This function will write the current players score to the MySQL table column named "score", the row in relation to the players UserID. UserID is a number that is generated when an account is created so that the user's account can be easily referred to.

I know its just me overthinking it, but I cannot seem to write a working piece of PHP code that I can link to the sendScore() function that sends the player's current score ('clicks' javascript variable) to their score column in the MySQL database.

Any help would be appreciated.

Thank you.

NOTE!!!!

The request to send info has to be an AJAX request. Maybe this is why it doesn't work. I am used to writing forms, but forms would refresh the page. Can anyone help write an AJAX request for this situation?

Caspar
  • 169
  • 2
  • 9
  • http://stackoverflow.com/questions/1968296/how-to-i-send-data-from-javascript-to-php-and-vice-versa This may be of help. In other words, use asynchronous Javascript to submit to a dummy form with a hidden input field that stores the score. However, be aware that your players may see this, and may try to fool the system. – Tiffany Jan 20 '17 at 22:43
  • @Tiffany Not exactly what I was looking for... this isn't to a mySQL table... thanks though!' – Caspar Jan 20 '17 at 22:48
  • https://www.formget.com/form-submission-using-ajax-php-and-javascript/ – Tiffany Jan 20 '17 at 22:57
  • If the above example isn't enough, [google has other examples](https://www.google.com/search?q=using+ajax+to+submit+form+to+mysql+database). – Tiffany Jan 20 '17 at 23:01

1 Answers1

0

What is your current sendScore function? It is kind of hard to give a detailed answer without more of your code, but I'll try: Your sendScore function will need to send a request to the PHP script that will handle this request. You can do that either using pure JavaScript, using XMLHttpRequest. More on that here: http://blog.garstasio.com/you-dont-need-jquery/ajax/. However, you can also use a framework such as jQuery to do some of the heavier lifting. More on jQuery and AJAX functions (specifically post-requests) can be found here: https://api.jquery.com/jQuery.post/.

Then you'll need a PHP function that can handle the request. You have to point your JavaScript function (that performs the POST request) to the URL of this script. Then you can process the information sent by your JavaScript just as you would process a 'normal' form.

To get your values from JavaScript to PHP and vice versa, it's probably easiest to use JSON, as both JavaScript and PHP handle JSON nicely these days, and it's a lot easier than XML.

  • I'm looking for an AJAX function but I can't seem to get it right... If you have the time, mind writing up the code? – Caspar Jan 20 '17 at 22:56
  • 2
    While I'm not going to write up the full code, if you post some more of your code right now, I can include some more specific pointers and suggestions so you can implement it, no problem. – Geert van Dijk Jan 20 '17 at 23:04