Using AJAX to Call PHP Methods on the Server
69Calling Server-Side PHP Methods Using AJAX
When developing web applications, you will often want to run some code on the server when the user does some action on the site. For example, when a user clicks a button, you may want to display a message that is generated by some code on the server. In order to do this, you will need to make a background call using Javascript that sends a message to the server and displays the response once it is returned by the server.
This can be easily accomplished using the Helix PHP Framework to handle the javascript calls and returns for you. To make it work, you simply add a button to the page and set the "onclick" property to an ajax method. You will specify the HTML element where the response should appear, and the name of the server side function to run the code.
This setup allows you to treat a web application like an event-driven application where events are raised by the user in the browser and they are handled on the server without ever reloading the page. The power of the Helix PHP Framework is that all of the underlying machinery is already written and tested, and the integration of the ExtJS Javascript framework ensures that it will work across all browsers.
The example below will show you how to call a function that shows the date using AJAX when the button is clicked.
The Markup
div::open("response");
echo "Response will appear here";
div::close();
$button = new button("Click Me");
$button->onclick = ajax("show_the_date","response");
echo $button;The Codebehind
function show_the_date()
{
echo date("d-F-Y");
}PrintShare it! — Rate it: up down flag this hub









