Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/183747-need-some-help-please/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.