PHP - 前端大量值傳至後端方式 2017-01-10
用過這個經驗是在大量刪除功能,
方式是這樣.
前端jQuery程式碼$("#m_delpart").click(function(){
//將checkbox轉入陣列中
var dect_checked = [];
$(".select_butt:checked").each(function(){
dect_checked.push($(this).val().replace('selected_' , '')); //將值推入dect_checked陣列中
});
//陣列單一變數化(將陣列變成以'xxx,xxx,xxx'的單一變數型態)
var split_checked;
split_checked = dect_checked.join(',');
if(split_checked != ''){
var submit_click = confirm("你確定要刪除所選取的訊息嗎?");
if(submit_click == true){
$.post("dowhat.php", { d_list: split_checked } , function(result){
$("#xline").append(result);
});
}
}
});
後端PHP處理方式$del_array = explode("," , $d_list); //將傳來的陣列分解
$count_array = count($del_array); //計算陣列總數
//將陣列寫入單一變數中
$del_target = "'";
for ( $i = 0 ; $i <= $count_array - 1 ; $i++){
if($i != $count_array - 1)
$del_target .= $del_array[$i] . "','";
else
$del_target .= $del_array[$i] . "'";
}