Solved

Best practice in how to organize ajax server-side pages

Posted on 2012-04-01
4
308 Views
Last Modified: 2012-06-03
I am using an old phpbb framework for a website.  We've only just 'discovered' AJAX and are intending using it to update the site a bit.

With phpbb the basic file structure for each page of output is like this (eg: xyz.php)
1) main php files are in the root: \\xyz.php
2) functions are all in included files: \\include\function_xyz.php
3) template files are located: \\templates\myTheme\template_xyz.tpl

Is there a best practice (or even your own prejudice?) on where I should be putting functions/pages that I am calling from AJAX (eg: from the xyz.php HTML page)?

eg:
1) create a folder \\ajax\ajax_xyz.php, and call this from the UI
2) put the function which returns data to the ui in \\include\function_xyz.php, called from \\xyz.php, which in turn is called from the UI
3) etc etc...
0
Comment
Question by:PaultheBroker
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37793341
I think I would go with #1.  My instincts tell me that will give you better organization.  But leave the question open for a day or so - there will be other opinions and suggestions from other experts.
0
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37793543
I'm with Ray on this.
I'd go with #1.
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 37793960
Are you talking about what php script you would requestvia javascript xmlhttprequests? Requests you make would depend on the functionality you need and the data you want to retrieve. That would seldom be one php script per php page, but one per topic, functionality, etc.

Eg a cascaded choice of a users city could be done by:
1. Initially giving the choice for a continent.
2. After the user selects a continent, calling getcountries.php, returning a list of countries of the choosen continent.
3. After the user selects a country, calling getcities.php, returning a list of the cities of a choosen country.

That's already a case of calling back into two different php scripts.

These two methods could of course be part of one \\ajax\ajax_xyz.php analog to \\include\function_xyz.php, but why would things you request from UI not also be \\include\function_xyz.php and if you think OOP and make use of MVC design pattern, this could also be done for ajax, all ajax calls could go into one ajax controller, which redirects and reuses view and model components to generate html snippets or return some data in the form of json or xml instead of a php array.

Bye, Olaf.
0
 
LVL 6

Author Comment

by:PaultheBroker
ID: 37794197
I like the per topic suggestion - its is how the includes are organized (mostly!)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question