views:

78

answers:

2

I have the following two event happening when my "export" url is clicked. No matter how I try, I can't combine the two without receiving a "hey, you're missing a comma or semicolon somewhere"-error. Can anybody suggest a way to combine the two, or should I just leave them separate as they are now?

$('#export').click(function() {
     $.each(gSelectedMeds, 
      function(intIndex, objValue) {
       i=intIndex + 1;
       if(i>1) {string+='&';}
       string+='med'+i+'="'+objValue+'"';
      }
     )
      string += "&count="+i;
    }); 
    $('#export').click(function(){
     $.ajax({
      url: 'ajax-exportMeds.php?'+string,
      type: "GET",
      dataType: "text",
      success: 
       function(data){
        $('#dialog_layer').dialog({
         autoOpen: true,       
         bgiframe: true,
         modal: true,
         buttons: {
           "OK": 
           function() {
            $(this).dialog("close");
           }
         }
        })
       }
     })
    });
A: 

Did you try this?

function doEach() {
        $.each(gSelectedMeds, 
                function(intIndex, objValue) {
                        i=intIndex + 1;
                        if(i>1) {string+='&';}
                        string+='med'+i+'="'+objValue+'"';
                }
        )
                string += "&count="+i;
}

function doAjax(){
        $.ajax({
                url: 'ajax-exportMeds.php?'+string,
                type: "GET",
                dataType: "text",
                success: 
                        function(data){
                                $('#dialog_layer').dialog({
                                        autoOpen: true,                                                 
                                        bgiframe: true,
                                        modal: true,
                                        buttons: {
                                                 "OK": 
                                                        function() {
                                                                $(this).dialog("close");
                                                        }
                                        }
                                })
                        }
        })
    }

$('#export').click(function() {
  doEach();
  doAjax();
});

You should put meaningful names, too. Of course, you can refactor this code to be easier.

eKek0
no, I have not tried that
acedanger
this worked great, thanks for the idea!
acedanger
+1  A: 

I don't know if this will do it, but combining into one function eliminates the global "string" which might have been causing you problems.

$('#export').click(function() {
  $.each(gSelectedMeds, 
     function(intIndex, objValue) {
        i=intIndex + 1;
        if(i>1) {string+='&';}
        string+='med'+i+'="'+objValue+'"';
     }
   )

   string += "&count="+i;

    $.ajax({
            url: 'ajax-exportMeds.php?'+string,
            type: "GET",
            dataType: "text",
            success: function(data){
              $('#dialog_layer').dialog({
                autoOpen: true,                                                 
                bgiframe: true,
                modal: true,
                buttons: {
                  "OK": function() { $(this).dialog("close"); }
                   }      
                })
             }
    })
});
ndp
excellent idea as well. I like this one better. Thank you very much!!
acedanger