My-Bic = My Beagle is Cute!

My-BIC = Easy Ajax - NOW WITH PHP TO FIREBUG SUPPORT!

Current Features as of 1.0 - Visit the Features section for more info

  • Easy Forms - one function call will take all your form values and create a pretty little query string for you ajaxObj.getForm('formid');
  • JSON Client Side Encoding - Now you can send and receive JSON encoded data from client or server!
  • Network Down Protection - MyBic can help to detect when the server is down and will disable itself from making more calls
  • Submission Queue built-in - All of your AJAX requests will be sent in the correct order, all requests are stored in the queue to prevent overwriting
  • Debugging Aid - just set ajaxObj.debug=1; and you will get a little popup that gives you info on your current request/responses
  • Firebug Integration - Use the MyBic firefox extension to send debug data from your php scripts to firebug! No mess, no fuss!


The What

After tiring of over hyped ajax frameworks trying to hide the guts that make ajax programming fun I decided to share my recipe for easy to make ajax applications where you still have control over everything, but the setup of it all is handled for you. This is a basic state of mind system rather than a framework. I offer you 3 files and a design pattern that allows you to focus on making things happen rather than setting things up. This is designed for PHP4 and PHP5  

My-BIC provides support for XML, JSON and TEXT ajax transactions. My-BIC has also been tested to work with Safari, Firefox, IE and Opera web browsers


As of version 1.0 We now include the MYBIC DEBUGGER. You can use this amazing tool to send debug information from PHP straight to firebug.

The 3 files I provide that you'll need:

Client Side:

    mybic.js - Javascript class to create xmlhttprequest object and make server calls

Server Side:

    mybic_server.php - The front controller, all ajax requests will be sent to this page

    mybic_json.php - If JSON encoding is requested, the response from your class will be JSON encoded(Michal Migurski)

The How

My-BIC overview


Do you see that mybic_server.php page up there? That's the front controller that runs the server side. I provide 2 server side pieces and you supply the rest. Please visit the tutorials section to see how easy most things are. The high level overview is you make a xmlhttp request to a server page named "mybic_server.php" which reads a $_REQUEST['action'] variable to find out what class it needs to load. The mybic_server.php page will then try and include that file and it first will call the "is_authorized" method of your class to see if the user should be logged in or has access to that page (which you define). If that returns true then mybic_server.php calls return_response to return a string, array or object back. It then either json encodes that data or sends the raw data back to the javascript page. 

Click here to view a sample PHP class that works with My-Bic

That sample class defines two methods your classes should abide by... is_authorized() and return_response(). The is_authorized function may always return true if all your scripts are public. However, if you need to check cookie params or session data, is_authorized is the place to do it. For example if your script is only available to logged in users you can check the session data, then return false if they are not logged in. the return_response function should do whatever it has to to fulfill the ajax request. The query vars are passed into the constructor so you have full access to those as well.

Example: 

Here is all the javascript code you need to AJAXify your application

<script type="text/javascript" src="mybic.js"></script>

<script>

var ajaxObj = new XMLHTTP("mybic_server.php");

function postComment(comment) {

            ajaxObj.call("action=postComment&comment="+comment, 'ComRespDiv');

}

</script>

Thats it for the javascript! By using 'ComRespDiv' the contents of the ajax call will automagically go into that div's innerHTML for you


Server Side

When you make a call to the server side to the "mybic_server.php" page you will pass an action variable which is the name of a class that can handle the ajax request. The ajax server page will inspect the action variable then include and instantiate that class calling a method on that class. By default all data is JSON encoded. JSON encoding has some benefits over XML or TEXT ats which are discussed here. The nice thing about having a front controller style on the server side is you can add security layers in one file which get handled on every ajax request. Let's say someone doesn't like you and decides to write a script that sends bad data nonstop to all your ajax pages. If you had 10 separate server pages you'd have alot of pain the ass work to do to block the requests but by having it in one location you can add a function above the script to check for bad IP's or malicious attempts. 

Setup:

on the Server you just need to drop in "mybic_server.php" and "mybic_json.php" which will JSON encode your data for you.