jQuery send and display ajax result per line of textarea

Sumith Harshan

I have the form with text area.In the text area user can enter keywords per line.

enter image description here

I want to do like this. After click the button send ajax request to the server and get result of the first keyword.Then display/append it.After that finished send second ajax request for 2nd keyword.Like this for per each keyword.

I want to do this because of server response is little slow per keyword.If display all keyword output at once that is getting too many time to see the result. So in above method I think user no need to wait for see the all results.User can see one by one keyword output.

My code is like this and I'm using this in the wordpress plugin.Get result as json.

jQuery(document).ready(function () {

 jQuery('#get_seo_rank').click(function() {  

    var keywordLines = jQuery('#keywords').val().split(/\n/); 
    var current = 0;
    var total = keywordLines.length;


    for (var i=0; i < total; i++) {
            jQuery.ajax({
              type : "post",
              dataType : "json",
              url : process.php,
              data : {
                        action: "get_rank", 
                        keywords: keywordLines[i]
                    },

                  beforeSend:function(){

                  },
                  success:function(response){

                        if(response.type == "success") {
                           jQuery("#resultWrap").append(response.result);
                        }
                        else if(response.type == "error") {
                           jQuery("#resultWrap").html(response.result);

                        } else {
                             jQuery("#resultWrap").html(response.result);
                        }

                  },
                  error:function(response){

                  }


           }); 

    }  // end loop


}); // end click event

});

If anyone can help regarding this that would really appreciate.

Thank you very much !

Gaurav Kalyan

Create a nested ajax call function for dynamic keyword:-

  1. Store all the keywords in array, let suppose keyword_list and store var key_length = keyword_list.length

  2. Then use an iterative function to call the list:-

    function nestedAjaxCalls(array1 , length1){ //terminating codition if (length1 == 0){ return;} else{ $.ajax({ .... data:{action: "get_rank",keywords: keywordLines[array1.length - length1]}, success:function(response){ ... nestedAjaxCalls(array1,length1 -1); } }); } }

  3. When the event is triggered call this function nestedAjaxCalls(keyword_list, key_length)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related