0

New to this javascript and jquery have bee doing most of my coding in php, Have been banging my head against the wall for about a week now trying to get this to work, I have looked through out this site and googled many others with this issues!

here is my complete function I think it is pretty self explanatory

function get_all_items(config_d)
{
var con = config_d.split(',');

var cat = $("#category").val();
var color = $("#C"+cat+"").val();
var range = $("#range").val();
var total = $("#Vtotal").val();

var Prod_arr = "";
var Qty_arr = "";


$.get('tables/HTL_products.txt',  function(data) 
{

    var lines = data.split('\n');

    for (var i=0; i<=lines.length; i++)
    {
        var elements = lines[i].split('~');

        var el = elements[0].split('_');

        var Arange = el[0]; 
        var Aproduct = elements[0];
        var Aconfig = elements[1];
        var Acat = elements[2];
        var Acolor = elements[3];

        for (var c=0; c<con.length; c++)
        {
            var config22 = con[c];

            var config1 = config22.replace("X", "V");

            var Vchk = $('#'+range+config1+'').is(':checked');

            if (Vchk==true)
            {
                var config23 = config1;
                var config2 = config23.replace("V", "X");
                var q_id = "#Q_"+Arange+"_"+config2+"";
                var qty = $(""+q_id+"").val();

                if (qty=="") { qty=0; }
                var qq = parseInt(""+qty+"");
            }
            else
            {
                var config23 = config22;
                var q_id = "#Q_"+Arange+"_"+config22+"";
                var qty = $(""+q_id+"").val();

                if (qty=="") { qty=0; }
                var qq = parseInt(""+qty+"");
            }



            if (range==Arange && config23==Aconfig && cat==Acat && qq > 0 && color==Acolor )
            {
                // these are the two strings I wish to use outside of this function!!!
                var Prod_arr = Prod_arr+','+Aproduct;
                var Qty_arr = Qty_arr+','+qq;

            }
        }

    }
});     


// At the moment am only trying to display variables
    // but wish to use variables here
alert(""+Prod_arr+"~"+Qty_arr+""); 
    // the plan here to pass the variables and open a php page
    // window.open("test.php?total="+total+"&aa="+Prod_arr+"&qq="+Qty_arr+"","_self");    } 

I know this may be a bit clunky but it all seems to work except for the use of the variables at end of parent function??

Thanks in advance for any assistance

mikle103
  • 1
  • 2
  • 1
    well, declare your variables outside of function if you want to output them outside. – Victor Levin Sep 30 '15 at 03:38
  • Related: [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Jonathan Lonowski Sep 30 '15 at 03:40
  • @Zealander they are declared on lines 9 and 10 above! – mikle103 Sep 30 '15 at 04:28
  • @JonathanLonowski thanks will look through – mikle103 Sep 30 '15 at 04:29
  • Why are you re-declaring them inside the if loop ?? if (range==Arange && config23==Aconfig && cat==Acat && qq > 0 && color==Acolor ) { // these are the two strings I wish to use outside of this function!!! var Prod_arr = Prod_arr+','+Aproduct; var Qty_arr = Qty_arr+','+qq; } – Swaprks Sep 30 '15 at 04:32
  • Inside the if loop remove the var if (range==Arange && config23==Aconfig && cat==Acat && qq > 0 && color==Acolor ) { // these are the two strings I wish to use outside of this function!!! Prod_arr = Prod_arr+','+Aproduct; Qty_arr = Qty_arr+','+qq; } – Swaprks Sep 30 '15 at 04:33
  • @Swaprks thanks! Have tried and didn't work!! Sorry – mikle103 Sep 30 '15 at 05:58
  • @mikle103 Ok. Try another thing. Create a function say ABC( Prod_arr, Qty_arr) with the params you need and call this function from your if loop where the param values are set. Now in the function write your alert and check. – Swaprks Oct 01 '15 at 03:55

0 Answers0