Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2012
    Location
    Kalamazoo, MI
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Procedural PHP with AJAX

    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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).

  • #3
    New Coder
    Join Date
    Aug 2012
    Location
    Kalamazoo, MI
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much. You just cleared up some major questions I had regarding the topic.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •