Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Best practice in how to organize ajax server-side pages

Posted on 2012-04-01
4
Medium Priority
?
321 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 111

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 30

Accepted Solution

by:
Olaf Doschke earned 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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…
Suggested Courses

885 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