My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for

English, Русский, Deutsche, French, Italiano, Türkçe, Nederlandse


phpDays - flexible php5 framework based on the MVC and ORM design patterns.

Start now

  • About the project (features)
  • Changelog (changes between project versions)
  • Install the framework on your server and then configure a new project
  • Upgrade an application from a previous version
  • Quick start to get your first application up and running
  • Answers to your questions
  • All pages in English

Every day use

  • MVC - basic concepts necessary to work with the framework
  • Library class reference descriptions
  • Ajax support in your applications
  • Multisite - manage many sites from one framework installation

For project members

EnAjax  
Ajax in your application.
Lang-En
Updated Nov 30, 2009 by anton.danilchenko

Using JavaScript libraries

We recommend using jQuery - a simple and easy to understand JS library.

Client side

For more information see Ajax in jQuery.

Create a new file named /vaw/www/myblog/public/static/js/global.js with the following content:

$(document).ready(function(){

  $.ajax({
    url: 'http://localhost/myblog/blog/posts',
    dataType: 'json',
    success: function(data) {
      alert(data);
    }
  });

});

ATTENTION Be sure to specify dataType: 'json' for best appearance.

Include the script file in the template page /vaw/www/myblog/app/View/layout/inde.html within the head tag:

  <script type="text/javascript" src="/static/js/jquery.min.js"></script>
  <script type="text/javascript" src="/static/js/global.js"></script>

Now when the page loads, our JS script will connect to the http://localhost/myblog/blog/posts page and the result will be displayed in a popup window. To use this example you have to implement the server side part.

Server side

In your application, you should create a method with a name ending with AjaxAction instead of the usual Action. From this method, we return data that will be sent to the client as a response.

ATTENTION To send data in the JSON format, the result must be created as an array and then converted to JSON using the json_encode() function.

Create a new file named /vaw/www/myblog/app/Controller/Blog.php with a postsAjaxAction() method:

class Controller_Blog extends Days_Controller {
  /** Return data in JSON */
  public function postsAjaxAction() {
    // obtain records from the database and return in the form of a multidimensional array
    $data = array('name'=>'Jimmi', 'age'=>23, 'country'=>'USA');
    return json_encode($data);
  }
}

Now you can try to call http://localhost/myblog to see that it works. If all goes well - you'll see a message with the data that were returned by the method postsAjaxAction.


Sign in to add a comment
Powered by Google Project Hosting