开发者

Write in MySQL database when something happens?

开发者 https://www.devze.com 2023-03-08 15:18 出处:网络
I\'m building a game with javascript where you can walk and pick up things, right now it works fine but it doesn\'t save the things you have picked up on my database so when you quit the game you lose

I'm building a game with javascript where you can walk and pick up things, right now it works fine but it doesn't save the things you have picked up on my database so when you quit the game you lose your progress.

I need to save to my databa开发者_JS百科se every time the user walks into something (when a div has a certain position). Right now there is a big javascript IF that basically deletes the item from the map.

Is it possible to make the javascript run the PHP code that writes in the database? How could I do it?


Since Javascript is client-side code, you can't simply call PHP (server-side code) with it. What you can do is post to a page that runs PHP code, using AJAX for instance.


The way you would do it is simple.

Every time the player does an action, have your script make an ajax call to the server to save the location (or what ever you are saving). You can just add a function to the part of your code, so when it is called, it sends the request in the background and saves the value to your databse. The only thing if that you would have to split the ajax requests up, so you only do one every 5-10 seconds. If you dont do this, then you would basically DDoSing you server.


you need ajax

http://code.google.com/edu/ajax/tutorials/ajax-tutorial.html

0

精彩评论

暂无评论...
验证码 换一张
取 消