This demo is a simple MVC design pattern which demonstrates retrieving values through XMLHTTP Requests (Ajax).
This requirement was to retrieve each the votes for each color by an xmlhttp request, and update the view.
Further, to total the votes which have been retrieved on the page without performing an additional xmlhttp request.
The application contains a database of colors, and a database of votes for each color by city. Clicking on a color name will generate an XMLHTTP request to retrieve the total votes for that color from all cities, and update the table. Clicking on "Total" will generate a local total for the votes that are displayed on the page.
|index.php||a front controller and router|
|color_model.php||a model for the data|
|color_view.php||a view for the user|
|json_view.php||a json view for xmlhttp request|
|colors_dao_pdo_sqlite.php||a DAO for SQLite 3|
|colors_db.sqlite||the database(SQLite 3)|
|color_ajax.js||a JS library for XMLHTTP requests|
|json2.js||MSIE needs this|
The database tables behind the scenes are:
|The colors table|
|The votes table|
This demo is made without the use of frameworks.
Most well-formed frameworks today will take care of things like XSS, SQL Injection, CSRF, and user-input validation, so I have not bothered with them in the code for this small application.
Many things might have been quicker to do if I had used a JS framework like jQuery. Its concept of selectors for DOM elements, event binding through delegates, and xmlhttp request functions are very convenient.
Download the source code