2009-10-04 71 views
1

我试图在我的javascript中加载数组。我需要以某种格式将这个数组发送给一个我将要调用的PHP脚本。在下面的例子中,gSelectedMeds是我的数组。价值count将告诉PHP脚本有多少meds期望收到。我无法将数组中的数据转换为可以通过$ .ajax数据选项发送的格式。任何帮助将不胜感激!!试图从jQuery发送一个数组到PHP函数

下面的代码是给我的悲伤在此刻的部分是数据选项:

$('#export').click(function() { 
    $.ajax({ 
     url:'ajax-exportMeds.php', 
     data: 
      {"number":gSelectedMeds.length}, 
      $.each(gSelectedMeds, 
       function(intIndex, objValue){ 
        {"med"+intIndex:objValue}, 
       } 
      ), 
     type: "GET", 
     //dataType: "text", 
     success: function(data){ 
      $('p#allMeds').text(''); 
      $('a.bank').text(''); 
      //clear array, bank and storedList divs 
      $(this).text(''); 
      gSelectedMeds[] = ''; 
      //$('ul#storedList').fadeOut('fast'); 
      $('ul#storedList').text(''); 
      return false; 
     }, 
    }), 
}); 

回答

0

我最终串起了阵列,并在此我叫URL的末尾发送计数:

 
$('#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, 
         closeOnEscape: true, 
         buttons: { 
          "OK": function() { $(this).dialog("close"); } 
         }  
        }) 
      } 
     }) 
    }); 
+0

@FilmJ建议http://stackoverflow.com/questions/838845/having-problem-getting-the-posted-array-from-jquery/1713985#1713985 – dsummersl 2014-05-04 14:26:09

4

你应该发送的数据为json。然后,你可以在PHP中使用json_decode()阅读> = 5.2.0

+0

做我需要创建JSON先锋?我将如何使用'data'选项发送它? – acedanger 2009-10-04 17:50:12

+0

实际上,我只是使用'JSON.stringify',似乎迄今为止工作 – acedanger 2009-10-04 18:18:55