Solved

Best practice in how to organize ajax server-side pages

Posted on 2012-04-01
4
305 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 108

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now