topical media & game development

talk show tell print

mashup-delicious-06-example6-10-index.php / php



  <?php
  //bring in Sajax goodness
  require(<Sajax.php>);
  
  //this function calcualtes the square root of a given number
  function get_sqrt(number) {
      if (is_numeric(number))
              return sqrt(number);
      else
          return "Not a number!";
  }
  
  //initalize sajax and export the get_sqrt function
  sajax_init();
  sajax_export("get_sqrt");
  sajax_handle_client_request();
  ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
  <title>PHP Square Root Calculator - AJAX Version!</title>
  <script>
  <?
      //this brings in all the javascript SAJAX needs to do its thing
      sajax_show_javascript();
  ?>
  //called onKeyUp in the number input field
  function calculate() {
      var number;
      number = document.getElementById("number").value;
      x_get_sqrt(number, calculate_cb);
  }
  //called after calculate() is complete
  function calculate_cb(result) {
      document.getElementById("result").innerHTML = result;
  }
  </script>
  </head>
  <body>
  <form>
      Get the square root of:
      <input type="text" name="number" id="number" size="3" onKeyUp="javascript:calculate()" />
  </form>
  <div>Result: <span id="result">Input a number above!</span></div>
  </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.