JavaScript
  Home arrow JavaScript arrow Page 5 - JavaScript Remote Scripting: An AJAX-based...
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 
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

JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2005-10-26

    Table of Contents:
  • JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action
  • Looking at existing functions: the “sendRequest()” and “stateChecker()” functions at a glance
  • Displaying random codes: defining the “createDataContainer()” function
  • Dynamic link generation: taking a look at the “createLinks()” function
  • Server-side processing: generating four-digit random codes with PHP
  • Putting the pieces together: showing the application’s complete code

  • 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


    JavaScript Remote Scripting: An AJAX-based Random Code Generator in Action - Server-side processing: generating four-digit random codes with PHP


    (Page 5 of 6 )

    As you saw when I explained the function for generating rating links, each <a> element was responsible for triggering a GET request, in order to fetch the “process_input.php” file and obtain the corresponding random code. Considering this situation, let’s have a look at the structure of this file, which contains a few lines of PHP code. Its source code is as follows:

    function getRandomString($length=4){
        if(!is_int($length)||$length<1){
            trigger_error('Invalid length for random string');
            exit();
        }
        $chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $randstring='';
        $maxvalue=strlen($chars)-1;
        for($i=0;$i<$length;$i++){
            $randstring.=substr($chars,rand(0,$maxvalue),1);
        }
        return $randstring;
    }
    // start or resume session
    session_start();
    // store challenge value in session variable
    $_SESSION['challenge']=getRandomString();
    // send challenge value to client
    echo $_SESSION['challenge'];

    As you can see, I’ve defined a simple PHP function for generating four-digit random strings. After defining the pertinent function, I’ve stored this value in a “challenge” session variable, which will be utilized by the server to determine whether the user has entered the correct code when proceeding to rate an article.

    Of course, the most interesting thing here is how the server sends back the random value to the client. By simply echoing this value, the random code can be obtained through the “responseText” property that belongs to the “XMLHttpRequest object, and displayed accordingly.

    Having provided you with all the proper explanations of each function involved in the implementation of the code generating application, the next thing to be done is to show the full source code for each file that integrates the program. Therefore, join me in the next few lines to see how the complete program code looks.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Over the last part of the series, the remaining JavaScript functions that comprise...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT