Jump to content

Saving score to text file


Choeeey
 Share

Recommended Posts

I am trying to save the score a user gets in a phaser game to a text file, so that it can be accessed in a php file.

 

Is there any ideas on how to do this?

I need the text file to save in the same location as my phaser project and cannot seem to find a way to do this.

Link to comment
Share on other sites

If you are using jquery, you can do the following:

var jqxhr = $.ajax( {url: "example.php",data: JSON.stringify({"score":123}) // this will convert the object into a string })  .done(function() {    alert( "success" );  })  .fail(function() {    alert( "error" );  })  .always(function() {    alert( "complete" );  });

And your php file will be able to receive the data.

 

JSON is just another form of object

Link to comment
Share on other sites

If you are using jquery, you can do the following:

var jqxhr = $.ajax( {url: "example.php",data: JSON.stringify({"score":123}) // this will convert the object into a string })  .done(function() {    alert( "success" );  })  .fail(function() {    alert( "error" );  })  .always(function() {    alert( "complete" );  });

And your php file will be able to receive the data.

 

JSON is just another form of object

Thanks, but what if I am using javascript? I have never used jquery before and unsure how to put this into a javascript file?

Also, how is this then accessed in the php file?

Link to comment
Share on other sites

Just download and load jquery.min.js in your index.html

Then create a new function and paste/modify that code to fit your needs.

 

Edit:

 

Vanilla JS: http://stackoverflow.com/questions/6418220/javascript-send-json-object-with-ajax

That is great thanks, just a bit confused about accessing it in PHP - you have $_POST in there and I was sure that was only used if you have a form? I do not have one? Sorry if I sound silly here, think I have got myself all mixed up.

 

Then in PHP do something like:

<?php

$jsonReceiveData = json_encode($_POST);

echo $jsonReceiveData;

?> 

Link to comment
Share on other sites

' Uncaught ReferenceError: $ is not defined' means that jQuery wasn't loaded on the page. You'll need to add this script tag in the html above your game's script tag(s)

 

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>

Thanks that solved that issue,

 

however in the php file it says the json is empty when I echo:

echo  $jsonReceiveData;

I get '[]'
Link to comment
Share on other sites

If you are using jquery, you can do the following:

var jqxhr = $.ajax( {url: "example.php",data: JSON.stringify({"score":123}) // this will convert the object into a string })  .done(function() {    alert( "success" );  })  .fail(function() {    alert( "error" );  })  .always(function() {    alert( "complete" );  });

And your php file will be able to receive the data.

 

JSON is just another form of object

could you tell me how to access this is php please? :)

Link to comment
Share on other sites

I really don't mean to be rude but you are asking about PHP in an HTML5 forum in the Phaser section.

 

I recommend that you do some research of your own because this is a very popular topic, you can start with:

 

http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

http://stackoverflow.com/questions/20392036/send-data-to-mysql-with-ajax-jquery-php

 

Or just type "PHP and jQuery AJAX" in google.

 

Good luck!

Link to comment
Share on other sites

Choeeey, You'll have an easier time if you send your request with a key.

var jqxhr = $.ajax( {url: "example.php",data: { postKey : JSON.stringify({"score":123}) }  // this will convert the object into a string });

Then, on the php side you would access it like 

<?phpvar_dump(json_decode($_POST["postKey"]));?>

Alternatively, if you only want the score you can just something like this in javascript / jQuery

 

$.post('example.php', {score: 23});

 

and then in PHP its just like 

 

<?php

//Bad practice. You'll need to validate all user submitted data as a security measure but i'm not gonna go into detail on that.

$score = $_POST["score"];

 

?>

Link to comment
Share on other sites

I really don't mean to be rude but you are asking about PHP in an HTML5 forum in the Phaser section.

 

I recommend that you do some research of your own because this is a very popular topic, you can start with:

 

http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

http://stackoverflow.com/questions/20392036/send-data-to-mysql-with-ajax-jquery-php

 

Or just type "PHP and jQuery AJAX" in google.

 

Good luck!

My apologies for the misplaced forum post. I didn't mean to put it under the wrong section.

 

Please also do not think that I done no research of my own before posting. 

 

I appreciate it everyone's help, however found an alternative way without having to use ajax. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...