2010-04-14 129 views
0

的問題
我特林做一個Ajax請求PHP腳本,但是我有獲取數據到格式的問題,即PHP期待吧,PHP期待進來作爲數組像內陣列中的數據,幫助一個AJAX請求

Array 
(
    [cv_file] => Array 
     (
      [849649717] => Y 
      [849649810] => Y 
     ) 

    [save] => Save CVs 
) 

我有什麼企圖?

我已經在我的JavaScript試圖創建一個空數組,並用其作爲數組鍵,這樣的事情,

var cv_file = new Array(); 
$(".drag_check").draggable({helper:"clone", opacity:"0.5"}); 
$(".searchPage").droppable({ 
    accept:".drag_check", 
    hoverClass: "dropHover", 
    drop: function(ev, ui) { 
     var droppedItem = ui.draggable.children(); 
     cv_file = ui.draggable.children().attr('name'); 
     var link = ui.draggable.children().attr('name').substr(ui.draggable.children().attr('name').indexOf("[")+1, ui.draggable.children().attr('name').lastIndexOf("]")-8) 
     $.ajax({ 
      type:"POST", 
      url:"/search", 
      data:cv_file+"&save=Save CVs", 
      success:function(){ 
       alert(cv_file) 
       $('.shortList').append('<li><input type="checkbox" value="Y" class="checkbox" name="remove_cv['+link+']"/><a href="/cv/'+link+'">'+link+'</a></li>'); 
      }, 
      error:function() { 
       alert("Somthing has gone wrong"); 
      } 
     }); 

    } 
}); 

我的問題

我如何獲取數據到PHP期望的格式,我會很感激任何人都可以給予的幫助嗎?

編輯
在提醒什麼意見海報建議,我得到他之後,

cv_file[849649717]&save=Save CVs

謝謝

+1

在POST'ing前發出以下表達式的結果,並用結果編輯您的問題。 'cv_file +「&save =保存CVs」' - 即'window.alert(cv_file +「&save =保存CVs」);' – 2010-04-14 15:14:19

+0

在我的示例中添加了警報 – 2010-04-14 15:21:37

回答

0

你可以字符串化的陣列/對象,然後做一個json_decode在PHP端將字符串轉換回數組。

編輯:現在用正確的PHP函數。

1

要獲得在後端PHP文件中的PHP陣列的結果,你需要創建像繩子下面回發到服務器:

cv_file[]=849649717&cv_file[]=849649810&save=Save CVs 

我不認爲你可以做到創建使用此方法關聯數組,但上面的應該給你喜歡的$_POST以下的數組:

Array 
(
    [cv_file] => Array 
     (
      0 => 849649717 
      1 => 849649810 
     ) 

    [save] => Save CVs 
) 

然後,你可以做一個foreach:

foreach ($_POST['cv_file'] as $cv) 
{ 
    // Do something with $cv; 
}