Solved

Best practice in how to organize ajax server-side pages

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mcrypt_create_iv() is deprecated 4 76
converting numbers with php 3 32
phpmailer in WHILE loop - weird results 10 29
get domain with php 7 20
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

19 Experts available now in Live!

Get 1:1 Help Now