2013-07-18 35 views
0

我有一個頁面,有一堆的勾號框,這些都是用名稱=「一個數字」標識的。 當單擊一個按鈕時,我有一個JavaScript函數,可以創建一個新數組,其中打勾的方框數量。這會發送到'test.php'頁面。javascript新陣列 - > php陣列

它返回:

Array 
(
    [data] => [null,"47284","47282","47281","47280","47279","47278","47277","47276","47269"] 
) 

當我嘗試和爆炸這陣,我得到一個數組,字符串轉換錯誤。

我在做什麼錯?

我需要將這些數字分開,以便我可以爲每個數據執行SQL查詢。

JavaScript代碼:

<script> 
var selected = new Array(); 

function showMessage() { 
var selected = Array(); 

    $('input:checked').each(function() { 
     selected.push($(this).attr('name')); 
    }); 
    var answer = confirm('Are you sure you want to send these emails again?'); 
    if(answer) { 
    var jsonString = JSON.stringify(selected); 
    $.ajax({ 
     url: "../../test.php", 
     type: "POST", 
     data: {data : jsonString}, 
     success: function(data){ 
      alert(data); 
     } 
    }) 
    } 
} 
</script> 
<SCRIPT language="javascript"> 
$(function(){ 

    // add multiple select/deselect functionality 
    $("#selectallsent").click(function() { 
      $('.yes').prop('checked', this.checked); 
    }); 
    $("#selectallprogress").click(function() { 
      $('.no').prop('checked', this.checked); 
    }); 

    // if all checkbox are selected, check the selectall checkbox 
    // and viceversa 
    $(".yes").click(function(){ 

     if($(".yes").length == $(".yes:checked").length) { 
      $("#selectallsent").prop("checked", "checked"); 
     } else { 
      $("#selectallsent").removeAttr("checked"); 
     } 

    }); 
    $(".no").click(function(){ 

     if($(".no").length == $(".no:checked").length) { 
      $("#selectallprogress").prop("checked", "checked"); 
     } else { 
      $("#selectallprogress").removeAttr("checked"); 
     } 

    }); 
}); 
</script> 

test.php的代碼:

<? 
print_r($_POST); 
?> 

我知道,我需要做的事情更多test.php的頁面上,但我想知道那是什麼。

+0

你應該打印'data' post var。 –

+0

如果你試圖爆炸一個數組,那麼你的問題。爆炸將一個字符串變成一個數組。 – Rooster

+0

那麼我需要做什麼來拆分這個數組呢?即時通訊疲憊和困惑,但我真的需要在我睡覺之前完成這件事。在此先感謝您的幫助 – fightapilotdean

回答

0

兩個你需要做的事情....

第一個是不是字符串化所選擇的對象(jQuery將處理這個你)

function showMessage() { 
var selected = Array(); 

    $('input:checked').each(function() { 
     selected.push($(this).attr('name')); 
    }); 
    var answer = confirm('Are you sure you want to send these emails again?'); 
    if(answer) { 
    $.ajax({ 
     url: "../../test.php", 
     type: "POST", 
     data: {data : selected}, 
     success: function(data){ 
      alert(data); 
     } 
    }) 
    } 
} 

然後在PHP端即可只需通過訪問值

print_r($_POST["data"]); 

這將是您的輸入數組。

+0

謝謝,Orangepill和NB這兩項工作都可以證實我現在可以做一個sql查詢,它使用來自mysql的'IN'也許iim讓我自己感到困惑,或者我只是笨拙的,但這似乎並不可能的 – fightapilotdean

+0

只是'foreach($ _POST [「data」]作爲$ id){/ *使用$ id * /}做你的查詢,你很好。 – Orangepill

+0

我考慮使用這個,但作爲用戶可能會選擇多個複選框,我覺得使用IN會更好,因爲它爲每個ID做1個sql而不是1個 – fightapilotdean