0
this.formsValidation = function(form_id)
{
    var fieldNames;
    jQuery.post('index.php','option=com_itcs_forms&controller=itcs_fields&task=getFieldsNameForValidation&tmpl=component&form_id='+form_id,
    function(data)
    {
        fieldNames = data;
    });
    alert(fieldNames);
    return false;
}

here "fieldNames" is showing "undefined" although it must show a string which is in "data". i am unable to store "data" to a variable, so that i can work with it after "post" function. how to do that?

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
arnas
  • 83
  • 8
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Musa Sep 13 '13 at 10:23
  • use firebug or other console in chrome or even i ie. Put break point and see what is in the data, data.response is maybe where your data is. – Vladimir Bozic Sep 13 '13 at 10:24
  • 2
    Your formatting, it hurts my eyes... When asking a question, please do try to format your code so that it is nicely indented and as readable as possible. Help people help you :) – Fluffeh Sep 13 '13 at 10:27

2 Answers2

1

Do the following -

this.formsValidation = function(form_id)
{
    var fieldNames;
    jQuery.post('index.php','option=com_itcs_forms&controller=itcs_fields&task=getFieldsNameForValidation&tmpl=component&form_id='+form_id, 
     function(data)
     {
         fieldNames = data;
         alert(fieldNames);
     });

     return false;
}
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
0

Your assignment should work, but your code is executing asynchronously - that is, you are issuing your AJAX call, then immediately executing alert() before the AJAX call has completed. If you wish to work with the data you should do so in the AJAX callback, thereby guaranteeing you have data when your code executes:

var fieldNames;
jQuery.post('index.php','option=com_itcs_forms&controller=itcs_fields&task=getFieldsNameForValidation&tmpl=component&form_id='+form_id,
function(data)
  {
     fieldNames = data;
     alert(fieldNames);
     // do more stuff here

  });
  • thank you all for assisting, i have got my answer. the problem was with "ajax synchronization. http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call this is the answer – arnas Sep 13 '13 at 10:44