Re: Any Running Simple Ajax Sample for Php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If you aren't already, I recommend putting to use a JS library with Ajax
support.
Nothing else, they can reduce the browser compatibility overhead coding
you'll need to do.

You can pick any of many, but I'm more familiar with Prototype:
http://prototypejs.org/

## credits.php
--------------------
<?php
/* ... */  // determine value for $credits
print min($credits, 0);
?>

## credits.html
---------------------
<html>
  <body>
    <div id="credits">Checking credits...</div>

    <script src="prototype.js"></script>
    <script type="text/javascript">
    /*** Reference ***/
    //// http://prototypejs.org/api/periodicalExecuter
    //// http://prototypejs.org/api/ajax/request
    //// http://prototypejs.org/api/element/observe

    function creditsCheck(pe) {
      new Ajax.Request('foobar.php', {
        method: 'get',
        parameters: {ts: (new Date()).getTime()}, // cache prevention
        onSuccess: function (transport) {
          var elem = $('credits');
          if (Number(transport.responseText) > 0) {
            elem.update('Credits remaining: ' + transport.responseText);
          } else {
            elem.update('Credits have expired.').setStyle({background:
'#f00'});
            pe.stop();
          }
        },
        onFailure: function () {
          $('credits').update('Could not determine
credits.').setStyle({background: '#f00'});
        }
      });
    }

    document.observe('dom:loaded', function () {
      var pe = new PeriodicalExecuter(creditsCheck, 60);
      creditsCheck(pe); // first check
    });
    </script>
  </body>
</html>

- Jon L.

On Fri, May 2, 2008 at 4:13 AM, Heysem KAYA <heysem.kaya@xxxxxxxxxxxxxxx>
wrote:

> Hi,
>
> I would like to check each minute whether the user credit is finished and
> update the relevant place on page. So I have downloaded however was not
> able
> to run some sample ajax code.
>
> Is there anyone who has such a simple code to implement?
>
>
>
> Thanks,
>
>
>
> Heysem Kaya
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux