Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

Thursday, October 31, 2013

Creating a Random Captcha with refresh Option

We all know that creating captcha is not a tough job in ColdFusion. But, one newbie in ColdFsion just asked me to help in creating a random captcha for his application.

I thought to share with others.

I have divided the entire code into three parts.

  1. captcha.cfm - Page where we will display the captcha image.
  2. Util.cfc - It contains a user defined function "generateRandomText" to generate random captcha text.
  3. refreshCaptcha.cfm - Page which will handle AJAX call while refreshing captcha image.

Lets see the "captcha.cfm" below:

Here, in above code I have used "ImageCreateCaptcha" function, which is added in ColdFusion 10 for captcha generation.

Lets see the "Util.cfc"

In this file, I have written a function called "generateRandomText"  for generating captcha string. Logic is very simple, you can modify it according to your requirement.

Next, come to "refreshCaptcha.cfm":

We are making AJAX call to this file which again generates random captcha image and sends to browser for display.

So, this is whole story for generating and refreshing captcha image in ColdFusion!!!

Friday, January 13, 2012

Asynchronous AJAX CALL Issue

I was working with some jQuery server side validation I got an issue of asynchronous AJAX call.
Let me describe with the following code:
 
//Validate Media path from server
$('input[name$="btnSubmit"]').click(function(){
   var stCurrentMediaPath = $('#dspForm_vcMedia_File_Path').val();
   var isValidPath = false;
  
    if(stCurrentMediaPath.length){
 
         //AJAX call to validate the Media path before form submission
        $.ajax({
            type: "GET",
            url: "index.cfm?inc=AddTaskAJAXListener&ajax=true",
            data: "method=validatePhysicalPath&vcPhysicalPath=" + escape(stCurrentMediaPath),
            statusCode: { 404: function() {
                alert('page not found');}
            },
            success:function(response){
                response = jQuery.trim(response).toLowerCase();
               
                if(response.toLowerCase() == 'yes'){
                    isValidPath = true;
                } else {
                    alert('Provided Media Path does not exist in system.');
                    isValidPath = false;
                }
            }
        });
        /*If the isValidPath is false then do the necessary action if true then submit the form. */
    }
});
 
But I got an issue: isValid is always false it takes the initial value which I have assigned at the time of declaring that variable. This is happening because our AJAX call is asynchronous call. The flow of execution of the block of code does not wait for the AJAX call to return. The flow initiates the AJAX call but does not wait for the result. So each time we will get isValid = false.

    So, our next step is how we can make the code flow linear execution(wait for AJAX call to finish). We can do that by making a synchronous AJAX call.

Please follow the block of code which will make the synchronous AJAX call:

//Validate Media path from server
$('input[name$="btnSubmit"]').click(function(){
   var stCurrentMediaPath = $('#dspForm_vcMedia_File_Path').val();
   var isValidPath = false;
   
    //Sync AJAX call to validate the Media path before form submission
    if(stcurrentMediaPath.length){
        $.ajax({
            async: false,//This part will make the synchronous AJAX call
            type: "GET",
            url: "index.cfm?inc=AddTaskAJAXListener&ajax=true",
            data: "method=validatePhysicalPath&vcPhysicalPath=" + escape(stCurrentMediaPath),
            statusCode: { 404: function() {
                alert('page not found');}
            },
            success:function(response){
                response = jQuery.trim(response).toLowerCase();
               
                if(response.toLowerCase() == 'yes'){
                    isValidPath = true;
                } else {
                    alert('Provided Media Path does not exist in system.');
                    isValidPath = false;
                }
            }
        });
        /*If the isValidPath is false then do the necessary action if true then submit the form. */
    }
});

Hope this will help you.

Followers