View Full Version : Procedural PHP with AJAX

10-30-2012, 09:47 PM
I am fairly new to PHP and thus not very experienced with the Object Oriented style. I am writing a web app in procedural PHP and I would like to add some AJAX functionality in it.

How can I use AJAX with procedural PHP? I have found MANY tutorials on how to use it with OOP, but that is not what I am trying to do. Can someone point me in the right direction of a tutorial or give me some tips/examples of AJAX with procedural PHP?

Any help/tips/examples are greatly appreciated!:thumbsup:

10-31-2012, 12:01 AM
The concept is still identical. JS -> script -> results to JS.
The end result of an AJAX lookup is simply the output of the script provided. The PHP script simply needs to output what the JS wants to get, whether it be simple plain text, JSON format or XML or whatever.
OOP is quite easy to write an object into XML as you can write a method or chain it to the __toString and print it out and it will convert the object properties into a nice little XML package. Arrays can do this just as easily, a simple recursive method to build the XML string.
Frankly, the PHP is the easy part of the AJAX equation. Write the PHP as you would normally and even allow it to function as a standard PHP script. You can use an input from get or post to indicate it is ajax and trim it down if it is (if desired, will help on the size of the output). Then AJAX simply asks for this page and parses the results. This is honestly the more difficult part, but so long as you know what the AJAX is planning to do with it, it is a relatively simple task to perform.

So what I'm getting at here is that OOP versus procedural is ultimately no different. Its the parsed response that you are interested in; how you get there is completely up to you. I'm a huge OOP fan, but procedural definitely has its place (and is less code, less memory intensive, and [more often than not] easier to do).

11-01-2012, 09:04 PM
Thank you very much. You just cleared up some major questions I had regarding the topic.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum