2009-10-04 58 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