Home arrow JavaScript arrow Page 3 - Running Queries in the Background with a MySQL Client with AJAX
JAVASCRIPT

Running Queries in the Background with a MySQL Client with AJAX


Are you interested in learning how to query different MySQL databases by using only a web browser as the execution environment? If your answer is yes, then look no further, because you’re in the right place. This is the second part of the series "Creating a MySQL client with AJAX." In three comprehensive tutorials, this series gives you some useful pointers on how to build quickly a MySQL client application that uses AJAX for sending queries in the background.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
September 05, 2006
TABLE OF CONTENTS:
  1. · Running Queries in the Background with a MySQL Client with AJAX
  2. · Creating the application's main panel
  3. · Working with HTTP requester objects
  4. · Initializing the user panel and displaying result sets
  5. · Putting all the pieces together

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Running Queries in the Background with a MySQL Client with AJAX - Working with HTTP requester objects
(Page 3 of 5 )

 

If you found creating the previous user panel a no-brainer process, then defining the set of JavaScript functions tasked with sending queries typed by the user and displaying the results is a slightly more complex procedure. However, if developing AJAX-based applications is quite familiar to you, the experience shouldn’t present major problems.

First, let me show you the handy “sendHttpRequest()” function, which is responsible for sending all the entered queries to the server. Its signature is as follows:

// send http requests
function sendHttpRequest(url,callbackFunc,respXml){
   
var xmlobj=null;
   
try{
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            xmlobj=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            alert('AJAX is not supported by your browser!');
            return false;
        }
   }
   xmlobj.onreadystatechange=function(){
        if(xmlobj.readyState==4){
            if(xmlobj.status==200){
                respXml?eval
(callbackFunc+'(xmlobj.responseXML)'):eval
(callbackFunc+'(xmlobj.responseText)');
            }
        }
    }
    // open socket connection
    xmlobj.open('GET',url,true);
    // send http header
    xmlobj.setRequestHeader('Content-Type','text/html;
charset=UTF-8');
    // send http request
    xmlobj.send(null);
}

  

As you can see, the above “sendHttpRequest()” function first creates, in a cross-browser fashion, an HTTP requester object, which is used later to send the query typed by the user. Also, after the corresponding request has been successfully processed, a callback function is called, either for handling a text-based response, that is by using the “responseText” property, or an XML-based result, in this case by utilizing the “responseXML” member.

Right, at this level you know how the application will send the queries entered by the user to the server, once the “Execute” button is clicked on. Therefore, the next step consists of defining the pertinent callback function that processes all the responses received from the server, as well as another one, tasked with initializing the main user panel that you saw in the previous section.

To see how all these tasks will be carried out, keep reading.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials