Choeeey Posted January 13, 2015 Share Posted January 13, 2015 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 More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 To be honest, I am actually open to other suggestions. I need to be able to access var score in drag.js, in a seperate php file. Link to comment Share on other sites More sharing options...
MichaelD Posted January 13, 2015 Share Posted January 13, 2015 What about a json call to the php script? Link to comment Share on other sites More sharing options...
Daniel Belohlavek Posted January 13, 2015 Share Posted January 13, 2015 Use AJAX to send the information to PHP and to write to a text file or a database. You could also use localStorage or sessionStorage (see Web storage API) and save the data in the client. Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 What about a json call to the php script? Could you expand on that please? Link to comment Share on other sites More sharing options...
MichaelD Posted January 13, 2015 Share Posted January 13, 2015 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 More sharing options...
Daniel Belohlavek Posted January 13, 2015 Share Posted January 13, 2015 Then in PHP do something like: <?php $jsonReceiveData = json_encode($_POST); echo $jsonReceiveData; ?> Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 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 objectThanks, 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 More sharing options...
Daniel Belohlavek Posted January 13, 2015 Share Posted January 13, 2015 Just download and load jquery.min.js in your index.htmlThen create a new function and paste/modify that code to fit your needs. Edit: JS only: http://stackoverflow.com/questions/6418220/javascript-send-json-object-with-ajax Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 Just download and load jquery.min.js in your index.htmlThen 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-ajaxThat 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 More sharing options...
Daniel Belohlavek Posted January 13, 2015 Share Posted January 13, 2015 @Choeeey POST is just one of several request methods in the HTTP protocol : http://en.wikipedia.org/wiki/POST_%28HTTP%29 $_POST contains the datajson_encode() parses it into JSONecho outputs it into the webpage You will have to look into PHP's file system functions if you wish to write to a txt file. Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 Thank you! I don't need it in a text file - having it just go to the php is perfect Daniel Belohlavek 1 Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 Just download and load jquery.min.js in your index.htmlThen create a new function and paste/modify that code to fit your needs. Edit: JS only: http://stackoverflow.com/questions/6418220/javascript-send-json-object-with-ajax Okay so I put the code in a function and it doesnt like it ' Uncaught ReferenceError: $ is not defined' Link to comment Share on other sites More sharing options...
tralf Posted January 13, 2015 Share Posted January 13, 2015 ' 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> Choeeey 1 Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 ' 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 More sharing options...
Daniel Belohlavek Posted January 13, 2015 Share Posted January 13, 2015 try $_POST["data"], I'm not sure it's been a long time since I touched PHP, sorry Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 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 objectcould you tell me how to access this is php please? Link to comment Share on other sites More sharing options...
Daniel Belohlavek Posted January 13, 2015 Share Posted January 13, 2015 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-phphttp://stackoverflow.com/questions/20392036/send-data-to-mysql-with-ajax-jquery-php Or just type "PHP and jQuery AJAX" in google. Good luck! MichaelD 1 Link to comment Share on other sites More sharing options...
tralf Posted January 13, 2015 Share Posted January 13, 2015 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"]; ?> Choeeey 1 Link to comment Share on other sites More sharing options...
Choeeey Posted January 13, 2015 Author Share Posted January 13, 2015 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-phphttp://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. Daniel Belohlavek 1 Link to comment Share on other sites More sharing options...
Filozofer Posted January 14, 2015 Share Posted January 14, 2015 Here is an example of what you're trying to achieve: (maybe too late^^)https://mega.co.nz/#!C4gSmDjJ!9RL-Rsa1_LbT7mK2gIU7TfH5zWavHmeQgybZdtckYXM But as @Belohlavek I strongly recommend you to find PHP tutorial ! Link to comment Share on other sites More sharing options...
Recommended Posts