JavaScript
  Home arrow JavaScript arrow Page 4 - Programmatic POST Requests with JavaScript...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Programmatic POST Requests with JavaScript: A Functional Form Emulator
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-07-27

    Table of Contents:
  • Programmatic POST Requests with JavaScript: A Functional Form Emulator
  • Building a functional script: listing the “getXMLHTTPObject()” and “sendRequest()” functions
  • Getting the form’s (X)HTML markup: defining the “getFormCode()” function
  • Getting form data: defining the “getFormAction()” and “getFormVariables()” functions
  • Generating random data: defining the “getRandomValue()” and “getRandomEmail()” functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Programmatic POST Requests with JavaScript: A Functional Form Emulator - Getting form data: defining the “getFormAction()” and “getFormVariables()” functions


    (Page 4 of 5 )

    As you might guess, getting the value for the form’s action attribute, as well as the field names, is pretty straightforward. First, here is the definition for the “getFormAction()” function, which not surprisingly obtains the form action’s value:

    // function getFormAction

    function getFormAction(){

        var formaction=document.getElementsByTagName('form')
    [0].getAttribute('action');

        if(!formaction){return};

        return formaction;

    }

    As you can see, the function simply uses the methods provided by the DOM to obtain the URL specified as the form’s action attribute, and returns its value. Here, the scripts makes a rough assumption, by considering that the requested page contains only one form. Anyway, if more forms are coded within the same page, it’s fairly easy to adapt the code for dealing with multiple forms.

    Once the program has obtained the URL where the form values will be processed, it needs to get the names of the corresponding fields, which will be populated with random strings. So, here is the list for the “getFormVariables()” function:

    // function getFormVariables

    function getFormVariables(){

        var formvars='';

        var childElements=document.getElementsByTagName('form')
    [0].childNodes;

        for(var i=0;i<childElements.length;i++){

                if(/(INPUT|TEXTAREA|SELECT)/.test(childElements
    [i].nodeName)){

    // check if field name contains the string 'email'
                                                      formvars+=(/mail/.test(childElements
    [i].getAttribute('name')))?childElements
    [i].getAttribute('name')+'='+getRandomEmail()
    +'&':childElements[i].getAttribute('name')
    +'='+getRandomValue()+'&';

                }

        }

        formvars=formvars.substring(0,formvars.length-1);

        return formvars;

    }

    Basically, what the above function does is obtain the names that correspond to the form fields. As they’re obtained trough a regular loop, they’re populated with random data. Notice that the function roughly attempts to find out if the field’s name contains the string “mail”. In this case, the form variable will be populated with a pseudo randomly-generated email address, which is obtained by calling the “getRandomEmail()” function.

    The expression listed above uses either the “getRandomEmail()” function, in the case of having a field that suggests entering an email address, or “getRandomValue()”, for populating other fields. Lastly, the function returns a string in the form of name=value pairs that will be sent as form data.

    As I said previously, don’t feel concerned about how those undefined functions look, since they will be reviewed over the next few lines. All you have to do is keep on reading.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This third part of the series covers in detail each relevant function for building a...
       · Happily Internet Explorer raises a warning dialog when you try to open a stream from...
       · Thanks for the comments.Yes, warning dialogs are good and fine for well-intented...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT