Sajax and PHP

Sajax and PHP Print E-mail
Contributed by Teddy   
Thursday, 21 September 2006

What is Sajax ?

Sajax is an Ajax framework that lets you create Ajax JavaScript on the server by using various server-side languages. The most recent version 0.12, you can download it from here

How does Sajax work?

You can use it on the server to create the JavaScript that will support Ajax in your browser. Currently, Sajax lets you connect to ASP, ColdFusion, Io, Lua, Perl, PHP, Python, and Ruby on the server.

A multiply example with Sajax and PHP

This example allow you to enter two values and click the Calculate button, the page uses Ajax to multiply the values on the server and display the result without a page refresh.See following figure.

  •  Create a file named multiply.php and include Sajax.php:

<?
require(“Sajax.php”);
...
?>

Define a PHP function named multiply to multiply two numbers

 function multiply($x, $y) {
  return $x * $y;
 }

Then set up Sajax by calling sajax_init, and export the multiply function:

 sajax_init();
 sajax_export("multiply");

Call the sajax_handle_client_request method

sajax_handle_client_request();

Generate the JavaScript

Sajax generates the JavaScript needed in this example by calling PHP function sajax_show_javascript,

<script>
<?
sajax_show_javascript();
?>

Full Source Code

<?
 require("Sajax.php"); 
 function multiply($x, $y) {
  return $x * $y;
 }
 sajax_init();
 // $sajax_debug_mode = 1;
 sajax_export("multiply");
 sajax_handle_client_request(); 
?>
<html>
<head>
 <title>Multiplier</title>
 <script>
 <?
 sajax_show_javascript();
 ?>
 function do_multiply_cb(z) {
  document.getElementById("z").value = z;
 }
 function do_multiply() {
  // get the folder name
  var x, y;
  x = document.getElementById("x").value;
  y = document.getElementById("y").value;
  x_multiply(x, y, do_multiply_cb);
 }
 </script>
</head>
<body>
 <input type="text" name="x" id="x" value="2" size="3">
 *
 <input type="text" name="y" id="y" value="3" size="3">
 =
 <input type="text" name="z" id="z" value="" size="3">
 <input type="button" name="check" value="Calculate"
  onclick="do_multiply(); return false;">
</body>
</html>


Realted Tutorials

A collection of AJAX tutorials 

Last Updated ( Thursday, 21 September 2006 )

  home              contact us

 

©2006-2010 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground