javascript - storing data sent back from PHP to ajax in global array -
i have built life expectancy calculator , want create graph. created array x stores values age. program sends each elements in x php using ajax , php send results.
i want store results in global array y can create graph. problem value assignment done inside success: function{}. elements discarded after that. below code:
age(); var yearage = $('input:text[name=ageyr]').val(); value = parsefloat(yearage); var x = []; var yval = []; var change = [-5,-4,-3,-2,-1,0,1,2,3,4,5]; var stop = false; (var = 0; <= 10; ++i) { x[i] = value +change[i]; } console.log(x); $('form.ajax').on('submit',function(event){ var =$(this), url = that.attr('action'), type = that.attr('method'), data ={}; var radio_button_value; if ($("input[name='sex']:checked").length > 0){ gender = $('input:radio[name=sex]:checked').val(); } that.find ('[name]').each(function(index,value){ var = $(this), name = that.attr('name'), value = that.val(); data[name] = value; }); (var j = 0; j <= 10; ++j){ var information = { 'sex': gender, 'ageyr' : x[j], 'agemo' :$('input:text[name=agemo]').val(), 'dateyr': $('input:text[name=dateyr]').val(), 'livepercent':$('input:text[name=livepercent]').val(), 'liveyr':$('input:text[name=liveyr]').val(), 'liveage':$('input:text[name=liveage]').val(), 'liveyrschg' : $('input:hidden[name=liveyrschg]').val() }; console.log(information); $.ajax({ url: url, type: type, data: information, datatype : "json", success: function(results){ var data = results; yval.push( data.lifeexpyr); console.log(yval); } }); } event.preventdefault(); });
this got:
["47.66"]
["47.66", "46.62"]
["47.66", "46.62", "45.57"]
["47.66", "46.62", "45.57", "44.52"]
["47.66", "46.62", "45.57", "44.52", "43.47"]
["47.66", "46.62", "45.57", "44.52", "43.47", "42.42"]
["47.66", "46.62", "45.57", "44.52", "43.47", "42.42", "41.38"]
["47.66", "46.62", "45.57", "44.52", "43.47", "42.42", "41.38", "40.33"]
["47.66", "46.62", "45.57", "44.52", "43.47", "42.42", "41.38", "40.33", "39.29"]
[["47.66", "46.62", "45.57", "44.52", "43.47", "42.42", "41.38", "40.33", "39.29", "38.25"]
["47.66", "46.62", "45.57", "44.52", "43.47", "42.42", "41.38", "40.33", "39.29", "38.25", "37.21"]
but outside of ajax yval empty ! please me, new javascript
Comments
Post a Comment