richarro1234 Posted December 2, 2009 Share Posted December 2, 2009 Hello, Ive just made a shop which is based on ajax/php and mysql. But when i got the response text back i need it to update the users money. So say someone has 10,000, the buy 3 items, the "buy" request is sent off via ajax to a seperate page, where it adds info to the database and takes off the cost from the money. then the responsetext comes back showing that the database has been updated. But what i need now is a script or a function that will update the money without re-loading the page either. Or that will show the money live, so that whenever someone buys something there money will automatically change Thanks Rich Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/ Share on other sites More sharing options...
KevinM1 Posted December 2, 2009 Share Posted December 2, 2009 Why can't you return the amount of money to be subtracted from the user in the response text? Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-969914 Share on other sites More sharing options...
richarro1234 Posted December 2, 2009 Author Share Posted December 2, 2009 because its in a different place. Its a game, so the money and user stats is in a bar at the top of the page. Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-969927 Share on other sites More sharing options...
KevinM1 Posted December 2, 2009 Share Posted December 2, 2009 because its in a different place. Its a game, so the money and user stats is in a bar at the top of the page. What does that have to do with anything? The response text comes from the server side script. You can have it return some JSON, then have the JavaScript portion retrieve the new money value from that and dynamically put it where it needs to be via DOM manipulation. Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-969944 Share on other sites More sharing options...
richarro1234 Posted December 2, 2009 Author Share Posted December 2, 2009 well i dont know how to do that, couldnt find any working examples. Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-969966 Share on other sites More sharing options...
KevinM1 Posted December 2, 2009 Share Posted December 2, 2009 well i dont know how to do that, couldnt find any working examples. http://lmgtfy.com/?q=ajax+json+response Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-969973 Share on other sites More sharing options...
richarro1234 Posted December 2, 2009 Author Share Posted December 2, 2009 sorry, thought i put i couldnt find any working Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-969981 Share on other sites More sharing options...
KevinM1 Posted December 3, 2009 Share Posted December 3, 2009 sorry, thought i put i couldnt find any working Did you look at any of the links? Processing JSON returned by the server essentially boils down to using either eval() (not recommended - see below) or JSON.parse to transform the string of the response text into an object. A quick example using jQuery: ajaxtest.html: <!DOCTYPE html> <html> <head> <title>Ajax Test</title> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $.post("quickajax.php", {action: "red"}, function(data) { var response = JSON.parse(data); //var response = eval('(' + data + ')'); $("#box1").html(response.id + " : " + response.cost); }); }); </script> <style type="text/css"> #box1 { border: 1px solid red; } </style> </head> <body> <div id="intro"> Trying a simple server request.... </div> <div id="box1"></div> </body> </html> The commented out line works, and is there to illustrate the other (dangerous) way to get at the object. My PHP is simply: quickajax.php: $action = $_POST['action']; if ($action == 'red') { echo '{"id" : "Red", "cost" : "2.49"}'; } Hardly thrilling. Again, after the JSON is parsed/eval'ed, you can treat it like any other JavaScript object. This means you can dynamically calculate one's remaining money (via response.cost, in my example) and stuff the result into any HTML element you want. Just be sure you use correct JSON syntax (http://json.org/index.html) when building your response text. Also, I'd avoid using eval() for this. Eval() will attempt to evaluate anything as executable JavaScript. That could lead to security headaches. JSON.parse will only parse (surprisingly) incoming JSON, and throw an exception otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/183747-need-some-help-please/#findComment-970853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.